20115 月28
Android:指定分辨率和清晰度的图片压缩方法源码
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 file File 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(); } }
本文地址:https://wizzer.cn/archives/1792 , 转载请保留.