package cn.com.magnity.magnitycx.sdk; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.Camera; import android.media.ExifInterface; import android.net.Uri; import android.view.SurfaceHolder; import cn.com.magnity.magnitycx.MagApplication; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Collections; import java.util.Comparator; import java.util.List; /* loaded from: classes.dex */ public class VisibleCameraHelper { private static volatile VisibleCameraHelper instance_; private Camera camera_; private final CameraSizeComparator sizeComparator = new CameraSizeComparator(); private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() { // from class: cn.com.magnity.magnitycx.sdk.VisibleCameraHelper.1 @Override // android.hardware.Camera.PictureCallback public void onPictureTaken(byte[] data, Camera camera) { try { Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); File myCaptureFile = new File(MagApplication.magParameter.mediaDir, "1.jpg"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); camera.stopPreview(); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } }; public int getOrientation(Context context, Uri photoUri) { Cursor cursor = context.getContentResolver().query(photoUri, new String[]{"_id", "orientation"}, null, null, null); if (cursor == null) { return 0; } cursor.moveToFirst(); int i = cursor.getInt(cursor.getColumnIndexOrThrow("orientation")); cursor.close(); return i; } public int getExifOrientation(String filepath) { int orientation; ExifInterface exif = null; try { ExifInterface exif2 = new ExifInterface(filepath); exif = exif2; } catch (IOException ex) { ex.printStackTrace(); } if (exif == null || (orientation = exif.getAttributeInt("Orientation", -1)) == -1) { return 0; } switch (orientation) { } return 0; } public static VisibleCameraHelper getInstance() { if (instance_ == null) { synchronized (VisibleCameraHelper.class) { if (instance_ == null) { instance_ = new VisibleCameraHelper(); } } } return instance_; } public boolean openCamera(int id) { if (Camera.getNumberOfCameras() < 1) { return false; } if (this.camera_ != null) { stopPreview(); closeCamera(); } try { this.camera_ = Camera.open(id); return this.camera_ != null; } catch (Exception e) { return false; } } public void closeCamera() { if (this.camera_ != null) { this.camera_.release(); this.camera_ = null; } } public Size setPreviewSize(int expectedWidth, float rate) { Camera.Parameters parameters; if (this.camera_ == null || (parameters = this.camera_.getParameters()) == null) { return null; } List list = parameters.getSupportedPreviewSizes(); Collections.sort(list, this.sizeComparator); Camera.Size size = null; int num = list.size(); int i = 0; while (i < num) { Camera.Size size2 = list.get(i); size = size2; if (size.width > expectedWidth && equalRate(size, rate)) { break; } i++; } if (i == num) { return null; } parameters.setPreviewSize(size.width, size.height); this.camera_.setParameters(parameters); return new Size(size.width, size.height); } public Size setPictureSize(int expectedWidth, float rate) { Camera.Parameters parameters; if (this.camera_ == null || (parameters = this.camera_.getParameters()) == null) { return null; } List list = parameters.getSupportedPictureSizes(); Collections.sort(list, this.sizeComparator); Camera.Size size = null; int num = list.size(); int i = 0; while (i < num) { Camera.Size size2 = list.get(i); size = size2; if (size.width > expectedWidth && equalRate(size, rate)) { break; } i++; } if (i == num) { return null; } parameters.setPictureSize(size.width, size.height); this.camera_.setParameters(parameters); return new Size(size.width, size.height); } private boolean equalRate(Camera.Size s, float rate) { float r = s.width / s.height; return ((double) Math.abs(r - rate)) <= 0.1d; } public boolean startPreview(SurfaceHolder holder, int flashMode) { String mode; if (this.camera_ == null) { return false; } stopPreview(); try { this.camera_.setPreviewDisplay(holder); Camera.Parameters parameters = this.camera_.getParameters(); if (parameters != null) { switch (flashMode) { case 0: mode = "off"; break; case 1: mode = "auto"; break; case 2: mode = "on"; break; case 3: mode = "red-eye"; break; case 4: mode = "torch"; break; default: mode = "off"; break; } parameters.setFlashMode(mode); this.camera_.setParameters(parameters); } this.camera_.startPreview(); return true; } catch (IOException e) { return false; } } public void stopPreview() { if (this.camera_ != null) { this.camera_.stopPreview(); } } public int setPreviewOrientation(int cameraId) { int result; if (this.camera_ == null) { return 0; } Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = MagApplication.windowManager.getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case 0: degrees = 0; break; case 1: degrees = 90; break; case 2: degrees = 180; break; case 3: degrees = 270; break; } if (info.facing == 1) { int result2 = (info.orientation + degrees) % 360; result = (360 - result2) % 360; } else { result = ((info.orientation - degrees) + 360) % 360; } this.camera_.setDisplayOrientation(result); return result; } public void takePicture() { if (this.camera_ != null) { this.camera_.takePicture(null, null, this.jpegCallback); } } private class CameraSizeComparator implements Comparator { private CameraSizeComparator() { } @Override // java.util.Comparator public int compare(Camera.Size lhs, Camera.Size rhs) { if (lhs.width == rhs.width) { return 0; } if (lhs.width > rhs.width) { return 1; } return -1; } } public class Size { private int height_; private int width_; public Size() { } public Size(int width, int height) { this.width_ = width; this.height_ = height; } public void setWidth(int width) { this.width_ = width; } public void setHeight(int height) { this.height_ = height; } public int getWidth() { return this.width_; } public int getHeight() { return this.height_; } } }