kopia lustrzana https://github.com/ryukoposting/Signal-Android
Ensure that camera captures have correct dimensions.
rodzic
c77809fa90
commit
dd66e22443
|
@ -230,8 +230,7 @@ public class CameraXFragment extends Fragment implements CameraFragment {
|
||||||
SimpleTask.run(CameraXFragment.this.getLifecycle(), () -> {
|
SimpleTask.run(CameraXFragment.this.getLifecycle(), () -> {
|
||||||
stopwatch.split("captured");
|
stopwatch.split("captured");
|
||||||
try {
|
try {
|
||||||
byte[] bytes = CameraXUtil.toJpegBytes(image, rotationDegrees, camera.getCameraLensFacing() == CameraX.LensFacing.FRONT);
|
return CameraXUtil.toJpeg(image, rotationDegrees, camera.getCameraLensFacing() == CameraX.LensFacing.FRONT);
|
||||||
return new CaptureResult(bytes, image.getWidth(), image.getHeight());
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -242,7 +241,7 @@ public class CameraXFragment extends Fragment implements CameraFragment {
|
||||||
stopwatch.stop(TAG);
|
stopwatch.stop(TAG);
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
controller.onImageCaptured(result.data, result.width, result.height);
|
controller.onImageCaptured(result.getData(), result.getWidth(), result.getHeight());
|
||||||
} else {
|
} else {
|
||||||
controller.onCameraError();
|
controller.onCameraError();
|
||||||
}
|
}
|
||||||
|
@ -255,16 +254,4 @@ public class CameraXFragment extends Fragment implements CameraFragment {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class CaptureResult {
|
|
||||||
public final byte[] data;
|
|
||||||
public final int width;
|
|
||||||
public final int height;
|
|
||||||
|
|
||||||
private CaptureResult(byte[] data, int width, int height) {
|
|
||||||
this.data = data;
|
|
||||||
this.width = width;
|
|
||||||
this.height = height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,9 @@ public class CameraXUtil {
|
||||||
|
|
||||||
private static final String TAG = Log.tag(CameraXUtil.class);
|
private static final String TAG = Log.tag(CameraXUtil.class);
|
||||||
|
|
||||||
|
@SuppressWarnings("SuspiciousNameCombination")
|
||||||
@RequiresApi(21)
|
@RequiresApi(21)
|
||||||
public static byte[] toJpegBytes(@NonNull ImageProxy image, int rotation, boolean flip) throws IOException {
|
public static ImageResult toJpeg(@NonNull ImageProxy image, int rotation, boolean flip) throws IOException {
|
||||||
ImageProxy.PlaneProxy[] planes = image.getPlanes();
|
ImageProxy.PlaneProxy[] planes = image.getPlanes();
|
||||||
ByteBuffer buffer = planes[0].getBuffer();
|
ByteBuffer buffer = planes[0].getBuffer();
|
||||||
Rect cropRect = shouldCropImage(image) ? image.getCropRect() : null;
|
Rect cropRect = shouldCropImage(image) ? image.getCropRect() : null;
|
||||||
|
@ -40,7 +41,17 @@ public class CameraXUtil {
|
||||||
data = transformByteArray(data, cropRect, rotation, flip);
|
data = transformByteArray(data, cropRect, rotation, flip);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
int width = cropRect != null ? (cropRect.right - cropRect.left) : image.getWidth();
|
||||||
|
int height = cropRect != null ? (cropRect.bottom - cropRect.top) : image.getHeight();
|
||||||
|
|
||||||
|
if (rotation == 90 || rotation == 270) {
|
||||||
|
int swap = width;
|
||||||
|
|
||||||
|
width = height;
|
||||||
|
height = swap;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ImageResult(data, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int toCameraDirectionInt(@Nullable CameraX.LensFacing facing) {
|
public static int toCameraDirectionInt(@Nullable CameraX.LensFacing facing) {
|
||||||
|
@ -119,4 +130,28 @@ public class CameraXUtil {
|
||||||
|
|
||||||
return out.toByteArray();
|
return out.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ImageResult {
|
||||||
|
private final byte[] data;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
public ImageResult(@NonNull byte[] data, int width, int height) {
|
||||||
|
this.data = data;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue