Fix camera rotation issues.

Had to manually detect when CameraX is giving us bad data.

Fixes #9509
This commit is contained in:
Greyson Parrelli 2020-03-30 15:58:37 -04:00
parent 87ea2f86c0
commit c9be37b84a
3 changed files with 113 additions and 3 deletions

View File

@ -366,7 +366,7 @@ public class CameraXFragment extends Fragment implements CameraFragment {
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(), () -> {

View File

@ -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);
}
}

View File

@ -14,6 +14,7 @@ import android.hardware.camera2.CameraCharacteristics;
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;
@ -26,12 +27,15 @@ import androidx.camera.core.ImageCapture;
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 {
@ -66,6 +70,20 @@ public class CameraXUtil {
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);
}
@ -84,7 +102,7 @@ public class CameraXUtil {
}
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) {