Skip to content

Commit

Permalink
Fix camera rotation issues.
Browse files Browse the repository at this point in the history
Had to manually detect when CameraX is giving us bad data.

Fixes #9509
  • Loading branch information
greyson-signal committed Mar 30, 2020
1 parent 87ea2f8 commit c9be37b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private void onCaptureClicked() {

camera.takePicture(Executors.mainThreadExecutor(), new ImageCapture.OnImageCapturedCallback() {
@Override
public void onCaptureSuccess(ImageProxy image) {
public void onCaptureSuccess(@NonNull ImageProxy image) {
flashHelper.endFlash();

SimpleTask.run(CameraXFragment.this.getViewLifecycleOwner().getLifecycle(), () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.thoughtcrime.securesms.mediasend.camerax;

import android.os.Build;

import java.util.HashSet;
import java.util.Set;

public final class CameraXModelBlacklist {
private static final Set<String> BLACKLIST = new HashSet<String>() {{
// Pixel 4
add("Pixel 4");
add("Pixel 4 XL");

// Huawei Mate 10
add("ALP-L29");
add("ALP-L09");
add("ALP-AL00");

// Huawei Mate 10 Pro
add("BLA-L29");
add("BLA-L09");
add("BLA-AL00");
add("BLA-A09");

// Huawei Mate 20
add("HMA-L29");
add("HMA-L09");
add("HMA-LX9");
add("HMA-AL00");

// Huawei Mate 20 Pro
add("LYA-L09");
add("LYA-L29");
add("LYA-AL00");
add("LYA-AL10");
add("LYA-TL00");
add("LYA-L0C");

// Huawei P20
add("EML-L29C");
add("EML-L09C");
add("EML-AL00");
add("EML-TL00");
add("EML-L29");
add("EML-L09");

// Huawei P20 Pro
add("CLT-L29C");
add("CLT-L29");
add("CLT-L09C");
add("CLT-L09");
add("CLT-AL00");
add("CLT-AL01");
add("CLT-TL01");
add("CLT-AL00L");
add("CLT-L04");
add("HW-01K");

// Huawei P30
add("ELE-L29");
add("ELE-L09");
add("ELE-AL00");
add("ELE-TL00");
add("ELE-L04");

// Huawei P30 Pro
add("VOG-L29");
add("VOG-L09");
add("VOG-AL00");
add("VOG-TL00");
add("VOG-L04");
add("VOG-AL10");

// Huawei Honor 10
add("COL-AL10");
add("COL-L29");
add("COL-L19");

// Samsung Galaxy S6
add("SM-G920F");

// Honor View 10
add("BLK-L09");
}};

private CameraXModelBlacklist() {
}

public static boolean isBlacklisted() {
return BLACKLIST.contains(Build.MODEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.os.Build;
import android.util.Pair;
import android.util.Rational;
import android.util.Size;

Expand All @@ -26,12 +27,15 @@
import androidx.camera.core.ImageProxy;

import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.mediasend.LegacyCameraModels;
import org.thoughtcrime.securesms.util.BitmapDecodingException;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.Stopwatch;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Locale;

public class CameraXUtil {

Expand Down Expand Up @@ -66,6 +70,20 @@ public static ImageResult toJpeg(@NonNull ImageProxy image, boolean flip) throws

buffer.get(data);

try {
Pair<Integer, Integer> dimens = BitmapUtil.getDimensions(new ByteArrayInputStream(data));

if (dimens.first != image.getWidth() && dimens.second != image.getHeight()) {
Log.w(TAG, String.format(Locale.ENGLISH, "Decoded image dimensions differed from stated dimensions! Stated: %d x %d, Decoded: %d x %d",
image.getWidth(), image.getHeight(), dimens.first, dimens.second));
Log.w(TAG, "Ignoring the stated rotation and rotating the crop rect 90 degrees (stated rotation is " + rotation + " degrees).");
rotation = 0;
cropRect = new Rect(cropRect.top, cropRect.left, cropRect.bottom, cropRect.right);
}
} catch (BitmapDecodingException e) {
Log.w(TAG, "Failed to decode!", e);
}

if (cropRect != null || rotation != 0 || flip) {
data = transformByteArray(data, cropRect, rotation, flip);
}
Expand All @@ -84,7 +102,7 @@ public static ImageResult toJpeg(@NonNull ImageProxy image, boolean flip) throws
}

public static boolean isSupported() {
return Build.VERSION.SDK_INT >= 21 && !LegacyCameraModels.isLegacyCameraModel();
return Build.VERSION.SDK_INT >= 21 && !CameraXModelBlacklist.isBlacklisted();
}

public static int toCameraDirectionInt(int facing) {
Expand Down

0 comments on commit c9be37b

Please sign in to comment.