?
/** * 按照比例放大或者缩小图片**/public static Bitmap scaleImage(Bitmap oldImage, int newW, int newH) { return scaleImage(oldImage,newW, newH,false); } public static Bitmap scaleImage(Bitmap oldImage, int newW, int newH,boolean isRecycleOld) { if (oldImage == null) return null; int oldWidth = oldImage.getWidth(); int oldHeight = oldImage.getHeight(); Matrix matrix = new Matrix(); matrix.postScale((float) newW / oldWidth, (float) newH / oldHeight); Bitmap resizedBitmap = Bitmap.createBitmap(oldImage, 0, 0, oldWidth, oldHeight, matrix, true); if(isRecycleOld){ oldImage.recycle(); } return resizedBitmap; }
/** * bitmap to byte[] * * @param bitmap * @return */ public static byte[] flattenBitmap(Bitmap bitmap) { if(bitmap ==null) return null; int size = bitmap.getWidth() * bitmap.getHeight() * 4; ByteArrayOutputStream out = new ByteArrayOutputStream(size); try { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); return out.toByteArray(); } catch (IOException e) { return null; } } /** * byte[] to bitmap * * @param bytes * @return */ public static Bitmap bytesToBitmap(byte[] bytes) { if (bytes == null) return null; return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); }?