Android:指定分辨率和清晰度的图片压缩方法源码
16lz
2021-01-23
http://www.wizzer.cn/?p=1792
public void transImage(String fromFile, String toFile, int width, int height, int quality){try{Bitmap bitmap = BitmapFactory.decodeFile(fromFile);int bitmapWidth = bitmap.getWidth();int bitmapHeight = bitmap.getHeight();// 缩放图片的尺寸float scaleWidth = (float) width / bitmapWidth;float scaleHeight = (float) height / bitmapHeight;Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// 产生缩放后的Bitmap对象Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);// save fileFile myCaptureFile = new File(toFile);FileOutputStream out = new FileOutputStream(myCaptureFile);if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){out.flush();out.close();}if(!bitmap.isRecycled()){bitmap.recycle();//记得释放资源,否则会内存溢出}if(!resizeBitmap.isRecycled()){resizeBitmap.recycle();}}catch (FileNotFoundException e){e.printStackTrace();}catch (IOException ex){ex.printStackTrace();}}
更多相关文章
- Android webView 支持缩放及自适应屏幕
- Android设备各种使用尺寸整理
- Android图片缩放、圆角处理以及倒影特效代码
- Android中的webView控件实现网页缩放
- 通过OpenGL ES混合模式缩放视频缓冲区-Android 4游戏编程高级篇
- Android 屏幕尺寸和分辨率的适配
- Android如何解决显示屏尺寸适配问题