mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 05:02:20 +00:00
committed by
Moxie Marlinspike
parent
47b21707be
commit
1a7ab6346f
@@ -100,7 +100,7 @@ public class BitmapUtil {
|
||||
|
||||
private static Bitmap createScaledBitmap(InputStream measure, InputStream orientationStream, InputStream data,
|
||||
int maxWidth, int maxHeight, boolean constrainedMemory)
|
||||
throws BitmapDecodingException
|
||||
throws IOException, BitmapDecodingException
|
||||
{
|
||||
Bitmap bitmap = createScaledBitmap(measure, data, maxWidth, maxHeight, constrainedMemory);
|
||||
return fixOrientation(bitmap, orientationStream);
|
||||
@@ -202,9 +202,9 @@ public class BitmapUtil {
|
||||
return scaler;
|
||||
}
|
||||
|
||||
private static Bitmap fixOrientation(Bitmap bitmap, InputStream orientationStream) {
|
||||
private static Bitmap fixOrientation(Bitmap bitmap, InputStream orientationStream) throws IOException {
|
||||
final int orientation = Exif.getOrientation(orientationStream);
|
||||
|
||||
orientationStream.close();
|
||||
if (orientation != 0) {
|
||||
return rotateBitmap(bitmap, orientation);
|
||||
} else {
|
||||
@@ -272,22 +272,65 @@ public class BitmapUtil {
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Bitmap createFromNV21(@NonNull final byte[] data,
|
||||
public static byte[] createFromNV21(@NonNull final byte[] data,
|
||||
final int width,
|
||||
final int height,
|
||||
final int rotation,
|
||||
int rotation,
|
||||
final Rect croppingRect)
|
||||
throws IOException
|
||||
{
|
||||
YuvImage previewImage = new YuvImage(data, ImageFormat.NV21,
|
||||
width, height, null);
|
||||
byte[] rotated = rotateNV21(data, width, height, rotation);
|
||||
final int rotatedWidth = rotation % 180 > 0 ? height : width;
|
||||
final int rotatedHeight = rotation % 180 > 0 ? width : height;
|
||||
YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21,
|
||||
rotatedWidth, rotatedHeight, null);
|
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
previewImage.compressToJpeg(croppingRect, 100, outputStream);
|
||||
previewImage.compressToJpeg(croppingRect, 80, outputStream);
|
||||
byte[] bytes = outputStream.toByteArray();
|
||||
outputStream.close();
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
return BitmapUtil.rotateBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length), rotation);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static byte[] rotateNV21(@NonNull final byte[] yuv,
|
||||
final int width,
|
||||
final int height,
|
||||
final int rotation)
|
||||
{
|
||||
if (rotation == 0) return yuv;
|
||||
if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
|
||||
throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
|
||||
}
|
||||
|
||||
final byte[] output = new byte[yuv.length];
|
||||
final int frameSize = width * height;
|
||||
final boolean swap = rotation % 180 != 0;
|
||||
final boolean xflip = rotation % 270 != 0;
|
||||
final boolean yflip = rotation >= 180;
|
||||
|
||||
for (int j = 0; j < height; j++) {
|
||||
for (int i = 0; i < width; i++) {
|
||||
final int yIn = j * width + i;
|
||||
final int uIn = frameSize + (j >> 1) * width + (i & ~1);
|
||||
final int vIn = uIn + 1;
|
||||
|
||||
final int wOut = swap ? height : width;
|
||||
final int hOut = swap ? width : height;
|
||||
final int iSwapped = swap ? j : i;
|
||||
final int jSwapped = swap ? i : j;
|
||||
final int iOut = xflip ? wOut - iSwapped - 1 : iSwapped;
|
||||
final int jOut = yflip ? hOut - jSwapped - 1 : jSwapped;
|
||||
|
||||
final int yOut = jOut * wOut + iOut;
|
||||
final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
|
||||
final int vOut = uOut + 1;
|
||||
|
||||
output[yOut] = (byte)(0xff & yuv[yIn]);
|
||||
output[uOut] = (byte)(0xff & yuv[uIn]);
|
||||
output[vOut] = (byte)(0xff & yuv[vIn]);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Bitmap createFromDrawable(final Drawable drawable, final int width, final int height) {
|
||||
|
||||
Reference in New Issue
Block a user