package com.epson.cameracopy.device;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import epson.common.ImageUtil;
import java.util.ArrayList;
import java.util.List;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class PortraitCameraView extends CameraBridgeViewBase implements Camera.PreviewCallback {
    @SuppressLint({"InlinedApi"})
    private static final String[] FAVORITE_FOCUS_MODES = {"continuous-video", "continuous-picture", "auto"};
    private static final int MAGIC_TEXTURE_ID = 10;
    private static final String TAG = "portCameraView";
    private byte[] mBuffer;
    protected Camera mCamera;
    protected JavaCameraFrame[] mCameraFrame;
    private boolean mCameraFrameReady = false;
    private int mChainIdx = 0;
    private Mat[] mFrameChain;
    private boolean mStopThread;
    private SurfaceTexture mSurfaceTexture;
    private Thread mThread;

    private boolean isInRange(double d, double d2, double d3) {
        return d - d2 <= d3 && d + d2 >= d3;
    }

    public static class JavaCameraSizeAccessor implements CameraBridgeViewBase.ListItemAccessor {
        public int getWidth(Object obj) {
            return ((Camera.Size) obj).width;
        }

        public int getHeight(Object obj) {
            return ((Camera.Size) obj).height;
        }
    }

    public PortraitCameraView(Context context, int i) {
        super(context, i);
    }

    public PortraitCameraView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }


    public org.opencv.core.Size calculateCameraFrameSize(java.util.List<?> r19, org.opencv.android.CameraBridgeViewBase.ListItemAccessor r20, int r21, int r22) {

        throw new UnsupportedOperationException("Method not decompiled: com.epson.cameracopy.device.PortraitCameraView.calculateCameraFrameSize(java.util.List, org.opencv.android.CameraBridgeViewBase$ListItemAccessor, int, int):org.opencv.core.Size");
    }

    private Camera.Size selectPreviewSize(List<Camera.Size> list, double d) {
        Camera.Size size = null;
        double d2 = 0.0d;
        for (Camera.Size next : list) {
            if (Math.abs((((double) next.width) / ((double) next.height)) - d) < 0.05d) {
                double d3 = ((double) (next.width * next.height)) / 307200.0d;
                if (d3 > 1.0d) {
                    d3 = 1.0d / d3;
                }
                if (d3 > d2) {
                    size = next;
                    d2 = d3;
                }
            }
        }
        return size;
    }

    protected boolean setPreviewSize(List<Camera.Size> list, Camera.Parameters parameters) {
        Point screenSize = getScreenSize();
        Camera.Size selectPreviewSize = selectPreviewSize(list, ((double) screenSize.y) / ((double) screenSize.x));
        if (selectPreviewSize == null) {
            return false;
        }
        parameters.setPreviewSize(selectPreviewSize.width, selectPreviewSize.height);
        return true;
    }

    private Point getScreenSize() {
        return ImageUtil.getDisplaySize(((Activity) getContext()).getWindowManager().getDefaultDisplay());
    }

    /* access modifiers changed from: protected */
    @SuppressLint({"NewApi"})
    public boolean initializeCamera(int i, int i2) {
        boolean z;
        Log.m52d(TAG, "Initialize java camera");
        synchronized (this) {
            mCamera = openBackCamera();
            z = false;
            if (mCamera == null) {
                return false;
            }
            try {
                Camera.Parameters parameters = mCamera.getParameters();
                Log.m52d(TAG, "getSupportedPreviewSizes()");
                List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
                if (supportedPreviewSizes != null) {
                    parameters.setPreviewFormat(17);
                    if (!setPictureAndPreviewResolutionManualMode(parameters)) {
                        setPreviewSize(supportedPreviewSizes, parameters);
                        setPictureSize(parameters.getPreviewSize(), parameters);
                    }
                    parameters.setJpegQuality(95);
                    int i3 = Build.VERSION.SDK_INT;
                    setForcusMode(parameters);
                    parameters.setRotation(90);
                    mCamera.setParameters(parameters);
                    Camera.Parameters parameters2 = mCamera.getParameters();
                    mFrameWidth = parameters2.getPreviewSize().height;
                    mFrameHeight = parameters2.getPreviewSize().width;
                    int i4 = mFrameHeight;
                    int i5 = mFrameWidth;
                    if (getLayoutParams().width == -1 && getLayoutParams().height == -1) {
                        mScale = Math.min(((float) i2) / ((float) mFrameHeight), ((float) i) / ((float) mFrameWidth));
                    } else {
                        mScale = 0.0f;
                    }
                    if (mFpsMeter != null) {
                        mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
                    }
                    mBuffer = new byte[(((mFrameWidth * mFrameHeight) * ImageFormat.getBitsPerPixel(parameters2.getPreviewFormat())) / 8)];
                    mCamera.addCallbackBuffer(mBuffer);
                    mCamera.setPreviewCallbackWithBuffer(this);
                    mFrameChain = new Mat[2];
                    mFrameChain[0] = new Mat((i5 / 2) + i5, i4, CvType.CV_8UC1);
                    mFrameChain[1] = new Mat(i5 + (i5 / 2), i4, CvType.CV_8UC1);
                    AllocateCache();
                    mCameraFrame = new JavaCameraFrame[2];
                    mCameraFrame[0] = new JavaCameraFrame(mFrameChain[0], mFrameWidth, mFrameHeight);
                    mCameraFrame[1] = new JavaCameraFrame(mFrameChain[1], mFrameWidth, mFrameHeight);
                    if (Build.VERSION.SDK_INT >= 11) {
                        mSurfaceTexture = new SurfaceTexture(10);
                        mCamera.setPreviewTexture(mSurfaceTexture);
                    } else {
                        mCamera.setPreviewDisplay((SurfaceHolder) null);
                    }
                    Log.m52d(TAG, "startPreview");
                    mCamera.startPreview();
                    z = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return z;
    }

    private static Camera openBackCamera() {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera camera = null;
        boolean z = false;
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == 0) {
                try {
                    camera = Camera.open(i);
                    z = true;
                } catch (RuntimeException e) {
                    Log.m53e(TAG, "Camera #" + i + "failed to open: " + e.getMessage());
                }
                if (z) {
                    return camera;
                }
            }
        }
        return null;
    }

    public boolean checkBackCamera() {
        disconnectCamera();
        Camera openBackCamera = openBackCamera();
        if (openBackCamera == null) {
            return false;
        }
        openBackCamera.release();
        return true;
    }

    private void setForcusMode(Camera.Parameters parameters) {
        boolean autoPictureMode = CameraPreviewControl.getInstance(getContext()).getAutoPictureMode();
        List<String> supportedFocusModes = parameters.getSupportedFocusModes();
        if (supportedFocusModes != null) {
            if (autoPictureMode || !supportedFocusModes.contains("auto")) {
                for (String str : FAVORITE_FOCUS_MODES) {
                    if (supportedFocusModes.contains(str)) {
                        parameters.setFocusMode(str);
                        return;
                    }
                }
                return;
            }
            parameters.setFocusMode("auto");
        }
    }

    private Point OLDselectPreviewSizeFromPictureSize(double d, List<Camera.Size> list) {
        int i;
        if (list == null || list.size() <= 0 || d <= 0.0d) {
            return null;
        }
        Point point = new Point();
        for (Camera.Size next : list) {
            if (next.height * next.width >= 76800 && Math.abs((((double) next.width) / ((double) next.height)) - d) < 0.05d && next.width > point.x) {
                point.x = next.width;
                point.y = next.height;
            }
        }
        if (point.x > 0) {
            return point;
        }
        int i2 = 0;
        Camera.Size size = list.get(0);
        point.x = size.width;
        point.y = size.height;
        double d2 = Double.MAX_VALUE;
        for (Camera.Size next2 : list) {
            if (next2.height > 0) {
                double abs = Math.abs((((double) next2.width) / ((double) next2.height)) - d);
                if (d2 > abs + 0.05d) {
                    i2 = next2.width * next2.height;
                    point.x = next2.width;
                    point.y = next2.height;
                    d2 = abs;
                } else if (d2 > abs - 0.05d && i2 < (i = next2.width * next2.height)) {
                    point.x = next2.width;
                    point.y = next2.height;
                    d2 = abs;
                    i2 = i;
                }
            }
        }
        return point;
    }

    private SizeWrapper selectPreviewSizeFromPictureSize(double d, List<Camera.Size> list) {
        if (list == null || d <= 0.0d) {
            return null;
        }
        CameraSizeList cameraSizeList = new CameraSizeList(list);
        int height = getHeight();
        int width = getWidth();
        SizeWrapper selectSize01 = PreviewResolutionSelector.selectSize01(d, cameraSizeList, height, width);
        if (selectSize01 != null) {
            return selectSize01;
        }
        return PreviewResolutionSelector.selectSize02(d, cameraSizeList, height, width);
    }

    private boolean setPictureAndPreviewResolutionManualMode(Camera.Parameters parameters) {
        CameraPreviewControl instance = CameraPreviewControl.getInstance(getContext());
        if (instance.getPictureResolutionMode() != 1) {
            return false;
        }
        Point pictureSize = instance.getPictureSize();
        if (pictureSize.x <= 0 || pictureSize.y <= 0) {
            return false;
        }
        parameters.setPictureSize(pictureSize.x, pictureSize.y);
        SizeWrapper selectPreviewSizeFromPictureSize = selectPreviewSizeFromPictureSize(((double) pictureSize.x) / ((double) pictureSize.y), parameters.getSupportedPreviewSizes());
        if (selectPreviewSizeFromPictureSize != null) {
            parameters.setPreviewSize(selectPreviewSizeFromPictureSize.getWidth(), selectPreviewSizeFromPictureSize.getHeight());
        }
        return true;
    }

    static void setPictureSize(Camera.Size size, Camera.Parameters parameters) {
        Camera.Size pictureSize01 = setPictureSize01(parameters.getSupportedPictureSizes(), ((double) size.width) / ((double) size.height));
        if (pictureSize01 != null) {
            parameters.setPictureSize(pictureSize01.width, pictureSize01.height);
        } else {
            setPictureSize02(size, parameters);
        }
    }

    static Camera.Size setPictureSize01(List<Camera.Size> list, double d) {
        int i = 0;
        Camera.Size size = null;
        for (Camera.Size next : list) {
            if (Math.abs((((double) next.width) / ((double) next.height)) - d) < 0.05d && i < next.width) {
                i = next.width;
                size = next;
            }
        }
        return size;
    }

    static void setPictureSize02(Camera.Size size, Camera.Parameters parameters) {
        List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();
        ArrayList arrayList = new ArrayList(supportedPictureSizes.size());
        for (Camera.Size next : supportedPictureSizes) {
            arrayList.add(new Point(next.width, next.height));
        }
        Point selectSize = selectSize(new Point(size.width, size.height), arrayList);
        if (selectSize != null) {
            parameters.setPictureSize(selectSize.x, selectSize.y);
        }
    }

    static Point selectSize(Point point, List<Point> list) {
        double d = ((double) point.x) / ((double) point.y);
        if (list == null) {
            return null;
        }
        double[] dArr = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Point point2 = list.get(i);
            dArr[i] = ((double) point2.x) * ((double) point2.y) * getRate(d, ((double) point2.x) / ((double) point2.y));
        }
        int maxIndex = getMaxIndex(dArr);
        if (maxIndex < 0 || maxIndex >= dArr.length) {
            return null;
        }
        return list.get(maxIndex);
    }

    static int getMaxIndex(double[] dArr) {
        if (dArr == null || dArr.length < 1) {
            return -1;
        }
        double d = dArr[0];
        int i = 0;
        for (int i2 = 0; i2 < dArr.length; i2++) {
            double d2 = dArr[i2];
            if (d < d2) {
                i = i2;
                d = d2;
            }
        }
        return i;
    }

    static double getRate(double d, double d2) {
        if (d <= 0.0d || d2 <= 0.0d) {
            return 1.0d;
        }
        double d3 = d2 / d;
        if (d3 > 1.0d) {
            d3 = 1.0d / d3;
        }
        return 1.0d / (Math.pow((d3 - 1.0d) * 10.0d, 2.0d) + 1.0d);
    }

    public Camera.Size getCameraPictureSize() {
        Camera camera = mCamera;
        if (camera == null) {
            return null;
        }
        try {
            Camera.Parameters parameters = camera.getParameters();
            if (parameters != null) {
                return parameters.getPictureSize();
            }
            return null;
        } catch (Exception unused) {
            return null;
        }
    }

    public List<Camera.Size> getPictureSizeList() {
        Camera camera = mCamera;
        if (camera == null) {
            return null;
        }
        try {
            Camera.Parameters parameters = camera.getParameters();
            if (parameters == null) {
                return null;
            }
            return parameters.getSupportedPictureSizes();
        } catch (Exception unused) {
            return null;
        }
    }

    protected void releaseCamera() {
        synchronized (this) {
            if (mCamera != null) {
                mCamera.stopPreview();
                mCamera.setPreviewCallback((Camera.PreviewCallback) null);
                mCamera.release();
            }
            mCamera = null;
            if (mFrameChain != null) {
                mFrameChain[0].release();
                mFrameChain[1].release();
            }
            if (mCameraFrame != null) {
                mCameraFrame[0].release();
                mCameraFrame[1].release();
            }
        }
    }

    protected boolean connectCamera(int i, int i2) {
        Log.m52d(TAG, "Connecting to camera");
        if (!initializeCamera(i, i2)) {
            return false;
        }
        mCameraFrameReady = false;
        Log.m52d(TAG, "Starting processing thread");
        mStopThread = false;
        mThread = new Thread(new CameraWorker(), "CameraWorker");
        mThread.start();
        return true;
    }

    protected void disconnectCamera() {
        Log.m52d(TAG, "Disconnecting from camera");
        try {
            mStopThread = true;
            Log.m52d(TAG, "Notify thread");
            synchronized (this) {
                notify();
            }
            Log.m52d(TAG, "Wating for thread");
            if (mThread != null) {
                mThread.join();
            }
        } catch (InterruptedException e) {
            try {
                e.printStackTrace();
            } catch (Throwable th) {
                mThread = null;
                throw th;
            }
        }
        mThread = null;
        releaseCamera();
        mCameraFrameReady = false;
    }

    public void onPreviewFrame(byte[] bArr, Camera camera) {
        synchronized (this) {
            mFrameChain[mChainIdx].put(0, 0, bArr);
            mCameraFrameReady = true;
            notify();
        }
        Camera camera2 = mCamera;
        if (camera2 != null) {
            camera2.addCallbackBuffer(mBuffer);
        }
    }

    private class JavaCameraFrame implements CameraBridgeViewBase.CvCameraViewFrame {
        private int mHeight;
        private Mat mRgba = new Mat();
        private Mat mRotated;
        private int mWidth;
        private Mat mYuvFrameData;

        public Mat gray() {
            Mat mat = mRotated;
            if (mat != null) {
                mat.release();
            }
            mRotated = mYuvFrameData.submat(0, mWidth, 0, mHeight);
            mRotated = mRotated.mo42753t();
            Mat mat2 = mRotated;
            Core.flip(mat2, mat2, 1);
            return mRotated;
        }

        public Mat rgba() {
            Imgproc.cvtColor(mYuvFrameData, mRgba, 96, 4);
            Mat mat = mRotated;
            if (mat != null) {
                mat.release();
            }
            mRotated = mRgba.mo42753t();
            Mat mat2 = mRotated;
            Core.flip(mat2, mat2, 1);
            return mRotated;
        }

        public JavaCameraFrame(Mat mat, int i, int i2) {
            mWidth = i;
            mHeight = i2;
            mYuvFrameData = mat;
        }

        public void release() {
            mRgba.release();
            Mat mat = mRotated;
            if (mat != null) {
                mat.release();
            }
        }
    }

    private class CameraWorker implements Runnable {
        private CameraWorker() {
        }

        public void run() {
            boolean z;
            do {
                synchronized (PortraitCameraView.this) {
                    while (!PortraitCameraView.mCameraFrameReady && !PortraitCameraView.mStopThread) {
                        try {
                            PortraitCameraView.this.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    z = false;
                    if (PortraitCameraView.mCameraFrameReady) {
                        int unused = PortraitCameraView.mChainIdx = 1 - PortraitCameraView.mChainIdx;
                        boolean unused2 = PortraitCameraView.mCameraFrameReady = false;
                        z = true;
                    }
                }
                if (!PortraitCameraView.mStopThread && z && !PortraitCameraView.mFrameChain[1 - PortraitCameraView.mChainIdx].empty()) {
                    PortraitCameraView portraitCameraView = PortraitCameraView.this;
                    portraitCameraView.deliverAndDrawFrame(portraitCameraView.mCameraFrame[1 - PortraitCameraView.mChainIdx]);
                }
            } while (!PortraitCameraView.mStopThread);
            Log.m52d(PortraitCameraView.TAG, "Finish processing thread");
        }
    }
}
