@Test public void canCropByteArray() throws ImageUtil.CodecFailedException { byte[] byteArray = ImageUtil.cropByteArray(mDataByteArray, new Rect(0, 0, CROP_WIDTH, CROP_HEIGHT)); Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); assertEquals(CROP_WIDTH, bitmap.getWidth()); assertEquals(CROP_HEIGHT, bitmap.getHeight()); }
public static byte[] cropByteArray(byte[] data, Rect cropRect) throws CodecFailedException { if (cropRect == null) { return data; } Bitmap bitmap = null; try { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(data, 0, data.length, false); bitmap = decoder.decodeRegion(cropRect, new BitmapFactory.Options()); decoder.recycle(); } catch (IllegalArgumentException e) { throw new CodecFailedException("Decode byte array failed with illegal argument." + e, CodecFailedException.FailureType.DECODE_FAILED); } catch (IOException e) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } if (bitmap == null) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); if (!success) { throw new CodecFailedException("Encode bitmap failed.", CodecFailedException.FailureType.ENCODE_FAILED); } bitmap.recycle(); return out.toByteArray(); }
ImageUtil { public static byte[] cropByteArray(byte[] data, Rect cropRect) throws CodecFailedException { if (cropRect == null) { return data; } Bitmap bitmap = null; try { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(data, 0, data.length, false); bitmap = decoder.decodeRegion(cropRect, new BitmapFactory.Options()); decoder.recycle(); } catch (IllegalArgumentException e) { throw new CodecFailedException("Decode byte array failed with illegal argument." + e, CodecFailedException.FailureType.DECODE_FAILED); } catch (IOException e) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } if (bitmap == null) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); if (!success) { throw new CodecFailedException("Encode bitmap failed.", CodecFailedException.FailureType.ENCODE_FAILED); } bitmap.recycle(); return out.toByteArray(); } }
ImageUtil { public static byte[] cropByteArray(byte[] data, Rect cropRect) throws CodecFailedException { if (cropRect == null) { return data; } Bitmap bitmap = null; try { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(data, 0, data.length, false); bitmap = decoder.decodeRegion(cropRect, new BitmapFactory.Options()); decoder.recycle(); } catch (IllegalArgumentException e) { throw new CodecFailedException("Decode byte array failed with illegal argument." + e, CodecFailedException.FailureType.DECODE_FAILED); } catch (IOException e) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } if (bitmap == null) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); if (!success) { throw new CodecFailedException("Encode bitmap failed.", CodecFailedException.FailureType.ENCODE_FAILED); } bitmap.recycle(); return out.toByteArray(); } private  ImageUtil(); }
ImageUtil { public static byte[] cropByteArray(byte[] data, Rect cropRect) throws CodecFailedException { if (cropRect == null) { return data; } Bitmap bitmap = null; try { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(data, 0, data.length, false); bitmap = decoder.decodeRegion(cropRect, new BitmapFactory.Options()); decoder.recycle(); } catch (IllegalArgumentException e) { throw new CodecFailedException("Decode byte array failed with illegal argument." + e, CodecFailedException.FailureType.DECODE_FAILED); } catch (IOException e) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } if (bitmap == null) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); if (!success) { throw new CodecFailedException("Encode bitmap failed.", CodecFailedException.FailureType.ENCODE_FAILED); } bitmap.recycle(); return out.toByteArray(); } private  ImageUtil(); static byte[] imageToJpegByteArray(ImageProxy image); static byte[] cropByteArray(byte[] data, Rect cropRect); static boolean isAspectRatioValid(Rational aspectRatio); static boolean isAspectRatioValid(Size sourceSize, Rational aspectRatio); static Rect computeCropRectFromAspectRatio(Size sourceSize, Rational aspectRatio); static Rational rotate(
            Rational rational, @RotationValue int rotation); }
ImageUtil { public static byte[] cropByteArray(byte[] data, Rect cropRect) throws CodecFailedException { if (cropRect == null) { return data; } Bitmap bitmap = null; try { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(data, 0, data.length, false); bitmap = decoder.decodeRegion(cropRect, new BitmapFactory.Options()); decoder.recycle(); } catch (IllegalArgumentException e) { throw new CodecFailedException("Decode byte array failed with illegal argument." + e, CodecFailedException.FailureType.DECODE_FAILED); } catch (IOException e) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } if (bitmap == null) { throw new CodecFailedException("Decode byte array failed.", CodecFailedException.FailureType.DECODE_FAILED); } ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); if (!success) { throw new CodecFailedException("Encode bitmap failed.", CodecFailedException.FailureType.ENCODE_FAILED); } bitmap.recycle(); return out.toByteArray(); } private  ImageUtil(); static byte[] imageToJpegByteArray(ImageProxy image); static byte[] cropByteArray(byte[] data, Rect cropRect); static boolean isAspectRatioValid(Rational aspectRatio); static boolean isAspectRatioValid(Size sourceSize, Rational aspectRatio); static Rect computeCropRectFromAspectRatio(Size sourceSize, Rational aspectRatio); static Rational rotate(
            Rational rational, @RotationValue int rotation);  }