旋转图片并保存
2015-12-16 16:18
691 查看
点击旋转控件实现图片的旋转及预览,点击保存控件将旋转后的图片存储起来
int current=0.0f;
Bitmap bigimage=compressImageFromFile(图片所在路径);
imageFiler为显示图片的控件ImageView
String strPath = Environment.getExternalStorageDirectory().getPath() + "/微途/" + System.currentTimeMillis() + ".jpg";
旋转控件的点击事件中:
current=current+90f;
Bitmap bm =rotaingImageView((int)current,bigimage);
imageFiler .setImageBitmap(bm);
保存控件的点击事件中:
saveImage();
int current=0.0f;
Bitmap bigimage=compressImageFromFile(图片所在路径);
imageFiler为显示图片的控件ImageView
String strPath = Environment.getExternalStorageDirectory().getPath() + "/微途/" + System.currentTimeMillis() + ".jpg";
旋转控件的点击事件中:
current=current+90f;
Bitmap bm =rotaingImageView((int)current,bigimage);
imageFiler .setImageBitmap(bm);
保存控件的点击事件中:
saveImage();
/** * * 压缩图片 * * @param srcPath * @return */ public Bitmap compressImageFromFile(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;// 只读边,不读内容 Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = 800f;// float ww = 480f;// int be = 1; if (w > h && w > ww) { be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) { be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;// 设置采样率 newOpts.inPreferredConfig = Config.ARGB_8888;// 该模式是默认的,可不设 newOpts.inPurgeable = true;// 同时设置才会有效 newOpts.inInputShareable = true;// 。当系统内存不够时候图片自动被回收 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); // return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩 // 其实是无效的,大家尽管尝试 return bitmap; }
//旋转图片 public Bitmap rotaingImageView(int angle , Bitmap bitmap) { //旋转图片 动作 Matrix matrix = new Matrix(); matrix.postRotate(angle); // 创建新的图片 return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); }
/** * 保存图片 */ private void saveImage() { Bitmap bitmap = ((BitmapDrawable)imageFiler.getDrawable()).getBitmap(); try { saveBitmapToFile(bitmap,strPath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.finish(); }
public void saveBitmapToFile(Bitmap bitmap, String _file) throws IOException { BufferedOutputStream os = null; try { File file = new File(_file); // String _filePath_file.replace(File.separatorChar + // file.getName(), ""); int end = _file.lastIndexOf(File.separator); String _filePath = _file.substring(0, end); File filePath = new File(_filePath); if (!filePath.exists()) { filePath.mkdirs(); } file.createNewFile(); os = new BufferedOutputStream(new FileOutputStream(file)); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
} finally { if (os != null) { try { os.close(); } catch (IOException e) { Log.e("保存失败", e.getMessage(), e); } } } }
相关文章推荐
- NodeJS+ExpressJS解决跨域请求CORS
- NSValue / NSNumber 拆包与封装
- R语言学习笔记之<在Linux上遇到的问题集锦>
- 几句禅语的解释
- appledoc 使用
- 为Qt Creator 开发astyle代码格式化插件
- 虚拟机ubuntu server安装VMwareTools,与windows共享文件
- 官网 webrtc_IOS 源码下载和编译
- 错误:java.lang.IllegalArgumentException:No view found for id for&nbsp
- Hexagon SDK安装过程指导
- 设计模式-工厂三姐妹
- 数据库db link
- centos7使用vnc viewer连接kvm虚拟机连接不上的解决方法
- easyui datagrid 去掉外边框及行与行之间的横线标题字体
- 常用iOS第三方库以及XCode插件介绍
- vi编辑器命令
- VS 正则表达式替换内容
- List<HashMap<String,String>> list, 根据hashmap中的某个键的值排序
- java图形界面设计1(AWT)
- 官网 webrtc_android 源码下载和编译