jimu-decompiled/sources/com/google/zxing/client/android/camera/CameraManager.java
2025-05-13 19:24:51 +02:00

233 lines
6.8 KiB
Java

package com.google.zxing.client.android.camera;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.client.android.camera.open.CameraFacing;
import com.google.zxing.client.android.camera.open.OpenCamera;
import com.google.zxing.client.android.camera.open.OpenCameraInterface;
import java.io.IOException;
/* loaded from: classes.dex */
public final class CameraManager {
private static final String m = "CameraManager";
private final Context a;
private final CameraConfigurationManager b;
private OpenCamera c;
private AutoFocusManager d;
private Rect e;
private Rect f;
private boolean g;
private boolean h;
private int i = -1;
private int j;
private int k;
private final PreviewCallback l;
public CameraManager(Context context) {
this.a = context;
this.b = new CameraConfigurationManager(context);
this.l = new PreviewCallback(this.b);
}
public synchronized void a(SurfaceHolder surfaceHolder) throws IOException {
OpenCamera openCamera = this.c;
if (openCamera == null) {
openCamera = OpenCameraInterface.a(this.i);
if (openCamera == null) {
throw new IOException("Camera.open() failed to return object from driver");
}
this.c = openCamera;
}
if (!this.g) {
this.g = true;
this.b.a(openCamera);
if (this.j > 0 && this.k > 0) {
a(this.j, this.k);
this.j = 0;
this.k = 0;
}
}
Camera a = openCamera.a();
Camera.Parameters parameters = a.getParameters();
String flatten = parameters == null ? null : parameters.flatten();
try {
this.b.a(openCamera, false);
} catch (RuntimeException unused) {
Log.w(m, "Camera rejected parameters. Setting only minimal safe-mode parameters");
Log.i(m, "Resetting to saved camera params: " + flatten);
if (flatten != null) {
Camera.Parameters parameters2 = a.getParameters();
parameters2.unflatten(flatten);
try {
a.setParameters(parameters2);
this.b.a(openCamera, true);
} catch (RuntimeException unused2) {
Log.w(m, "Camera rejected even safe-mode parameters! No configuration");
}
}
}
a.setPreviewDisplay(surfaceHolder);
}
public CameraFacing b() {
OpenCamera openCamera = this.c;
return openCamera != null ? openCamera.b() : CameraFacing.BACK;
}
public synchronized Rect c() {
if (this.e == null) {
if (this.c == null) {
return null;
}
Point b = this.b.b();
if (b == null) {
return null;
}
int a = a(b.x, 240, 1200);
int a2 = a(b.y, 240, 675);
int i = (b.x - a) / 2;
int i2 = (b.y - a2) / 2;
this.e = new Rect(i, i2, a + i, a2 + i2);
Log.d(m, "Calculated framing rect: " + this.e);
}
return this.e;
}
public synchronized Rect d() {
if (this.f == null) {
Rect c = c();
if (c == null) {
return null;
}
Rect rect = new Rect(c);
Point a = this.b.a();
Point b = this.b.b();
if (a != null && b != null) {
rect.left = (rect.left * a.x) / b.x;
rect.right = (rect.right * a.x) / b.x;
rect.top = (rect.top * a.y) / b.y;
rect.bottom = (rect.bottom * a.y) / b.y;
this.f = rect;
}
return null;
}
return this.f;
}
public int e() {
OpenCamera openCamera = this.c;
if (openCamera != null) {
return openCamera.c();
}
return 0;
}
public synchronized boolean f() {
return this.c != null;
}
public synchronized void g() {
OpenCamera openCamera = this.c;
if (openCamera != null && !this.h) {
openCamera.a().startPreview();
this.h = true;
this.d = new AutoFocusManager(this.a, openCamera.a());
}
}
public synchronized void h() {
if (this.d != null) {
this.d.b();
this.d = null;
}
if (this.c != null && this.h) {
this.c.a().stopPreview();
this.l.a(null, 0);
this.h = false;
}
}
public void b(int i) {
OpenCamera openCamera = this.c;
if (openCamera != null) {
openCamera.a().setDisplayOrientation(i);
}
}
public synchronized void a() {
if (this.c != null) {
this.c.a().release();
this.c = null;
this.e = null;
this.f = null;
}
}
public synchronized void a(boolean z) {
OpenCamera openCamera = this.c;
if (openCamera != null && z != this.b.a(openCamera.a())) {
boolean z2 = this.d != null;
if (z2) {
this.d.b();
this.d = null;
}
this.b.a(openCamera.a(), z);
if (z2) {
this.d = new AutoFocusManager(this.a, openCamera.a());
this.d.a();
}
}
}
public synchronized void a(Handler handler, int i) {
OpenCamera openCamera = this.c;
if (openCamera != null && this.h) {
this.l.a(handler, i);
openCamera.a().setOneShotPreviewCallback(this.l);
}
}
private static int a(int i, int i2, int i3) {
int i4 = (i * 9) / 20;
return i4 < i2 ? i2 : i4 > i3 ? i3 : i4;
}
public synchronized void a(int i) {
this.i = i;
}
public synchronized void a(int i, int i2) {
if (this.g) {
Point b = this.b.b();
if (i > b.x) {
i = b.x;
}
if (i2 > b.y) {
i2 = b.y;
}
int i3 = (b.x - i) / 2;
int i4 = (b.y - i2) / 2;
this.e = new Rect(i3, i4, i + i3, i2 + i4);
Log.d(m, "Calculated manual framing rect: " + this.e);
this.f = null;
} else {
this.j = i;
this.k = i2;
}
}
public PlanarYUVLuminanceSource a(byte[] bArr, int i, int i2) {
Rect d = d();
if (d == null) {
return null;
}
return new PlanarYUVLuminanceSource(bArr, i, i2, d.left, d.top, d.width(), d.height(), false);
}
}