public class BitmapToSizeChangeFile { /** * 将bitmap转换为file存储起来 * @param bitmap * @return */ public static File bitmapChangeFile(Bitmap bitmap) { FileOutputStream fileOutStream = null; File file = null; try { //通过相关方法生成一个Bitmap类型的对象,生产文件选择用当前事件的long型作为文件路径 file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".png"); fileOutStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutStream); // 把位图输出到指定的文件中 fileOutStream.flush(); } catch (Exception e) { e.printStackTrace(); }finally { try { fileOutStream.close(); } catch (IOException e) { e.printStackTrace(); } } return file; } /** * 处理图片 * @param bm 所要转换的bitmap * @param newWidth 新的宽 * @param newHeight 新的高 * @return 指定宽高的bitmap */ public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){ // 获得图片的宽高 int width = bm.getWidth(); int height = bm.getHeight(); // 计算缩放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片 Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); return newbm; } }
设置bitmap的宽高,同时将bitmap转换为file对象
最新推荐文章于 2023-04-27 19:46:32 发布