package com.epson.cameracopy.device;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.util.AttributeSet;

public class OpenCvCameraView extends PortraitCameraView implements Camera.PictureCallback, Camera.ShutterCallback, Camera.AutoFocusCallback {
    private static final String TAG = "OpenCvCameraView";
    private AutoFocusCallBack mAutoFocusCallBack;
    private boolean mHasAutoFocus;
    private volatile boolean mInAutofocus;
    private String mPictureFileName;
    private MyPictureTakeCallback mPictureTakeCallback;
    private boolean mPictureTaking;
    private boolean mTakePictuerAutofocus;

    public interface AutoFocusCallBack {
        void onAutoFocusCompleted(boolean z);
    }

    public interface MyPictureTakeCallback {
        void onMyPictureTaken(boolean z, String str);
    }

    public void onShutter() {
    }

    public void setDisplayOriantation(int i) {
    }

    public boolean isInAutofocus() {
        return this.mInAutofocus;
    }

    public OpenCvCameraView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            this.mHasAutoFocus = packageManager.hasSystemFeature("android.hardware.camera.autofocus");
        }
    }

    public Camera getCamera() {
        return this.mCamera;
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x0037 A[Catch:{ all -> 0x0061 }] */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x003c A[SYNTHETIC, Splitter:B:20:0x003c] */
    /* JADX WARNING: Removed duplicated region for block: B:27:0x0052  */
    /* JADX WARNING: Removed duplicated region for block: B:30:0x0057  */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x0064 A[SYNTHETIC, Splitter:B:35:0x0064] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void onPictureTaken(byte[] r4, android.hardware.Camera r5) {
        /*
            r3 = this;
            android.hardware.Camera r5 = r3.mCamera
            r5.startPreview()
            android.hardware.Camera r5 = r3.mCamera
            r5.setPreviewCallback(r3)
            r5 = 0
            r0 = 0
            java.io.FileOutputStream r1 = new java.io.FileOutputStream     // Catch:{ IOException -> 0x0025, all -> 0x0022 }
            java.lang.String r2 = r3.mPictureFileName     // Catch:{ IOException -> 0x0025, all -> 0x0022 }
            r1.<init>(r2)     // Catch:{ IOException -> 0x0025, all -> 0x0022 }
            r1.write(r4)     // Catch:{ IOException -> 0x0020 }
            r1.close()     // Catch:{ IOException -> 0x001a }
            goto L_0x001e
        L_0x001a:
            r4 = move-exception
            r4.printStackTrace()
        L_0x001e:
            r4 = 1
            goto L_0x0045
        L_0x0020:
            r4 = move-exception
            goto L_0x0027
        L_0x0022:
            r4 = move-exception
            r1 = r5
            goto L_0x0062
        L_0x0025:
            r4 = move-exception
            r1 = r5
        L_0x0027:
            r4.printStackTrace()     // Catch:{ all -> 0x0061 }
            java.io.File r4 = new java.io.File     // Catch:{ all -> 0x0061 }
            java.lang.String r2 = r3.mPictureFileName     // Catch:{ all -> 0x0061 }
            r4.<init>(r2)     // Catch:{ all -> 0x0061 }
            boolean r2 = r4.exists()     // Catch:{ all -> 0x0061 }
            if (r2 == 0) goto L_0x003a
            r4.delete()     // Catch:{ all -> 0x0061 }
        L_0x003a:
            if (r1 == 0) goto L_0x0044
            r1.close()     // Catch:{ IOException -> 0x0040 }
            goto L_0x0044
        L_0x0040:
            r4 = move-exception
            r4.printStackTrace()
        L_0x0044:
            r4 = 0
        L_0x0045:
            java.io.File r1 = new java.io.File
            java.lang.String r2 = r3.mPictureFileName
            r1.<init>(r2)
            boolean r1 = r1.exists()
            if (r1 != 0) goto L_0x0053
            r4 = 0
        L_0x0053:
            com.epson.cameracopy.device.OpenCvCameraView$MyPictureTakeCallback r1 = r3.mPictureTakeCallback
            if (r1 == 0) goto L_0x005e
            java.lang.String r2 = r3.mPictureFileName
            r1.onMyPictureTaken(r4, r2)
            r3.mPictureTakeCallback = r5
        L_0x005e:
            r3.mPictureTaking = r0
            return
        L_0x0061:
            r4 = move-exception
        L_0x0062:
            if (r1 == 0) goto L_0x006c
            r1.close()     // Catch:{ IOException -> 0x0068 }
            goto L_0x006c
        L_0x0068:
            r5 = move-exception
            r5.printStackTrace()
        L_0x006c:
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.cameracopy.device.OpenCvCameraView.onPictureTaken(byte[], android.hardware.Camera):void");
    }

    public synchronized void takePicture(String str, MyPictureTakeCallback myPictureTakeCallback) {
        this.mPictureTaking = true;
        if (!this.mInAutofocus) {
            this.mInAutofocus = true;
            this.mPictureFileName = str;
            this.mPictureTakeCallback = myPictureTakeCallback;
            if (!this.mHasAutoFocus) {
                this.mCamera.setPreviewCallback((Camera.PreviewCallback) null);
                this.mCamera.takePicture(this, (Camera.PictureCallback) null, this);
                this.mPictureTaking = false;
                this.mInAutofocus = false;
                return;
            }
            this.mTakePictuerAutofocus = true;
            localAutofocus();
        }
    }

    private void localAutofocus() {
        if (!this.mHasAutoFocus) {
            this.mInAutofocus = false;
            return;
        }
        try {
            this.mCamera.autoFocus(this);
        } catch (Exception unused) {
            onAutoFocus(false, this.mCamera);
        }
    }

    private void localCancelAutofocus() {
        if (!this.mHasAutoFocus) {
            this.mInAutofocus = false;
        } else if (this.mCamera != null) {
            this.mCamera.cancelAutoFocus();
        }
    }

    public void onAutoFocus(boolean z, Camera camera) {
        if (this.mTakePictuerAutofocus) {
            this.mTakePictuerAutofocus = false;
            this.mCamera.setPreviewCallback((Camera.PreviewCallback) null);
            this.mCamera.takePicture(this, (Camera.PictureCallback) null, this);
        } else {
            AutoFocusCallBack autoFocusCallBack = this.mAutoFocusCallBack;
            if (autoFocusCallBack != null) {
                autoFocusCallBack.onAutoFocusCompleted(z);
                this.mAutoFocusCallBack = null;
            }
        }
        this.mInAutofocus = false;
    }

    public synchronized void doAutofocus(AutoFocusCallBack autoFocusCallBack) {
        if (!this.mInAutofocus && !this.mPictureTaking) {
            this.mAutoFocusCallBack = autoFocusCallBack;
            this.mInAutofocus = true;
            localAutofocus();
        }
    }

    public void cancelAutofocus() {
        localCancelAutofocus();
        this.mInAutofocus = false;
        this.mAutoFocusCallBack = null;
        this.mTakePictuerAutofocus = false;
        this.mPictureTaking = false;
        this.mPictureTakeCallback = null;
    }

    public void enableView() {
        super.enableView();
        this.mInAutofocus = false;
        this.mPictureTaking = false;
    }

    public float getScale() {
        if (this.mScale <= 0.0f) {
            return 1.0f;
        }
        return this.mScale;
    }
}
