Ensure that camera captures have correct dimensions.

fork-5.53.8
Greyson Parrelli 2019-07-18 16:10:59 -04:00
rodzic c77809fa90
commit dd66e22443
2 zmienionych plików z 39 dodań i 17 usunięć

Wyświetl plik

@ -230,8 +230,7 @@ public class CameraXFragment extends Fragment implements CameraFragment {
SimpleTask.run(CameraXFragment.this.getLifecycle(), () -> {
stopwatch.split("captured");
try {
byte[] bytes = CameraXUtil.toJpegBytes(image, rotationDegrees, camera.getCameraLensFacing() == CameraX.LensFacing.FRONT);
return new CaptureResult(bytes, image.getWidth(), image.getHeight());
return CameraXUtil.toJpeg(image, rotationDegrees, camera.getCameraLensFacing() == CameraX.LensFacing.FRONT);
} catch (IOException e) {
return null;
} finally {
@ -242,7 +241,7 @@ public class CameraXFragment extends Fragment implements CameraFragment {
stopwatch.stop(TAG);
if (result != null) {
controller.onImageCaptured(result.data, result.width, result.height);
controller.onImageCaptured(result.getData(), result.getWidth(), result.getHeight());
} else {
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;
}
}
}

Wyświetl plik

@ -27,8 +27,9 @@ public class CameraXUtil {
private static final String TAG = Log.tag(CameraXUtil.class);
@SuppressWarnings("SuspiciousNameCombination")
@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();
ByteBuffer buffer = planes[0].getBuffer();
Rect cropRect = shouldCropImage(image) ? image.getCropRect() : null;
@ -40,7 +41,17 @@ public class CameraXUtil {
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) {
@ -119,4 +130,28 @@ public class CameraXUtil {
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;
}
}
}