导航菜单

页面标题

页面副标题

AdaModal v1.4.0 - IdCardCameraActivity.java 源代码

正在查看: AdaModal v1.4.0 应用的 IdCardCameraActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.common.fine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Size;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureException;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.common.fine.IdCardCameraActivity;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class IdCardCameraActivity extends AppCompatActivity {
    private static final int MAX_FILE_SIZE = 512;
    private static final int MAX_HEIGHT = 1080;
    private static final int MAX_WIDTH = 1920;
    private static final int REQUEST_CAMERA_PERMISSION = 1234;
    private static final int REQUEST_CODE_PERMISSIONS = 10;
    private static final String[] REQUIRED_PERMISSIONS = {"android.permission.CAMERA"};
    private Button btnCancel;
    private Button btnCapture;
    private Button btnConfirm;
    private Button btnRetake;
    private ExecutorService cameraExecutor;
    private LinearLayout captureLayout;
    Bitmap idCardBitmap;
    private Rect idCardRect;
    private ImageCapture imageCapture;
    private File photoFile;
    private ImageView previewImage;
    private LinearLayout previewLayout;
    private PreviewView previewView;
    private int screenHeight;
    private int screenWidth;
    private TextureView textureView;
    private TextView tvInstruction;
    private ViewfinderOverlay viewfinderOverlay;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setFlags(1024, 1024);
        setContentView(R.layout.activity_id_card_camera);
        setRequestedOrientation(0);
        initViews();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        this.screenWidth = displayMetrics.widthPixels;
        int i = displayMetrics.heightPixels;
        this.screenHeight = i;
        int i2 = (int) (i * 1.0f);
        int i3 = (int) (i2 / 1.5851852f);
        int i4 = (this.screenWidth - i2) / 2;
        int i5 = (i - i3) / 2;
        Rect rect = new Rect(i4, i5, i2 + i4, i3 + i5);
        this.idCardRect = rect;
        this.viewfinderOverlay.setIdCardRect(rect);
        if (ContextCompat.checkSelfPermission(this, "android.permission.CAMERA") != 0) {
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.CAMERA"}, REQUEST_CAMERA_PERMISSION);
        } else {
            startCamera();
        }
        this.cameraExecutor = Executors.newSingleThreadExecutor();
    }

    private void initViews() {
        this.previewView = (PreviewView) findViewById(R.id.previewView);
        this.viewfinderOverlay = (ViewfinderOverlay) findViewById(R.id.viewfinderOverlay);
        this.captureLayout = (LinearLayout) findViewById(R.id.captureLayout);
        this.previewLayout = (LinearLayout) findViewById(R.id.previewLayout);
        this.previewImage = (ImageView) findViewById(R.id.previewImage);
        this.btnCancel = (Button) findViewById(R.id.btnCancel);
        this.btnCapture = (Button) findViewById(R.id.btnCapture);
        this.btnConfirm = (Button) findViewById(R.id.btnConfirm);
        this.btnRetake = (Button) findViewById(R.id.btnRetake);
        this.tvInstruction = (TextView) findViewById(R.id.tvInstruction);
        this.btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                IdCardCameraActivity.this.lambda$initViews$0(view);
            }
        });
        this.btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                IdCardCameraActivity.this.lambda$initViews$1(view);
            }
        });
        this.btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                IdCardCameraActivity.this.lambda$initViews$2(view);
            }
        });
        this.btnRetake.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                IdCardCameraActivity.this.lambda$initViews$3(view);
            }
        });
    }

    public void lambda$initViews$0(View view) {
        finish();
    }

    public void lambda$initViews$1(View view) {
        takePhoto();
    }

    public void lambda$initViews$2(View view) {
        returnPhotoResult();
    }

    public void lambda$initViews$3(View view) {
        this.previewLayout.setVisibility(8);
        this.captureLayout.setVisibility(0);
    }

    private void startCamera() {
        final ListenableFuture<ProcessCameraProvider> processCameraProvider = ProcessCameraProvider.getInstance(this);
        processCameraProvider.addListener(new Runnable() {
            @Override
            public final void run() {
                IdCardCameraActivity.this.lambda$startCamera$4(processCameraProvider);
            }
        }, ContextCompat.getMainExecutor(this));
    }

    public void lambda$startCamera$4(ListenableFuture listenableFuture) {
        try {
            ProcessCameraProvider processCameraProvider = (ProcessCameraProvider) listenableFuture.get();
            Preview build = new Preview.Builder().setTargetAspectRatio(1).setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
            this.imageCapture = new ImageCapture.Builder().setCaptureMode(1).setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).setTargetResolution(new Size(MAX_WIDTH, MAX_HEIGHT)).build();
            CameraSelector build2 = new CameraSelector.Builder().requireLensFacing(1).build();
            processCameraProvider.unbindAll();
            processCameraProvider.bindToLifecycle(this, build2, build, this.imageCapture);
            build.setSurfaceProvider(this.previewView.getSurfaceProvider());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

    private void takePhoto() {
        try {
            this.photoFile = new File(getFilesDir(), new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()) + ".jpg");
            this.imageCapture.m170lambda$takePicture$4$androidxcameracoreImageCapture(new ImageCapture.OutputFileOptions.Builder(this.photoFile).build(), ContextCompat.getMainExecutor(this), new AnonymousClass1());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class AnonymousClass1 implements ImageCapture.OnImageSavedCallback {
        AnonymousClass1() {
        }

        @Override
        public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {
            IdCardCameraActivity.this.runOnUiThread(new Runnable() {
                @Override
                public final void run() {
                    IdCardCameraActivity.AnonymousClass1.this.lambda$onImageSaved$0();
                }
            });
        }

        public void lambda$onImageSaved$0() {
            try {
                IdCardCameraActivity.this.processCapturedImage();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(IdCardCameraActivity.this, "image err0", 0).show();
            }
        }

        @Override
        public void onError(ImageCaptureException imageCaptureException) {
            Log.e("IdCardCamera", "image err1: " + imageCaptureException.getMessage());
            Toast.makeText(IdCardCameraActivity.this, "image err1", 0).show();
        }
    }

    public void processCapturedImage() throws IOException {
        int width;
        int i;
        int i2;
        int i3;
        Bitmap decodeFile = BitmapFactory.decodeFile(this.photoFile.getAbsolutePath());
        float width2 = this.previewView.getWidth() / this.previewView.getHeight();
        if (width2 > decodeFile.getWidth() / decodeFile.getHeight()) {
            i = decodeFile.getWidth();
            i2 = (int) (i / width2);
            i3 = (decodeFile.getHeight() - i2) / 2;
            width = 0;
        } else {
            int height = decodeFile.getHeight();
            int i4 = (int) (height * width2);
            width = (decodeFile.getWidth() - i4) / 2;
            i = i4;
            i2 = height;
            i3 = 0;
        }
        Bitmap createBitmap = Bitmap.createBitmap(decodeFile, width, i3, i, i2);
        float width3 = createBitmap.getWidth() / this.previewView.getWidth();
        float height2 = createBitmap.getHeight() / this.previewView.getHeight();
        int i5 = (int) (this.idCardRect.left * width3);
        int i6 = (int) (this.idCardRect.top * height2);
        Bitmap createBitmap2 = Bitmap.createBitmap(createBitmap, i5, i6, ((int) (this.idCardRect.right * width3)) - i5, ((int) (this.idCardRect.bottom * height2)) - i6);
        this.idCardBitmap = createBitmap2;
        this.previewImage.setImageBitmap(createBitmap2);
        this.captureLayout.setVisibility(8);
        this.previewLayout.setVisibility(0);
    }

    private void compressAndSaveBitmap(Bitmap bitmap, File file) {
        int i = 100;
        do {
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                try {
                    bitmap.compress(Bitmap.CompressFormat.JPEG, i, fileOutputStream);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (Throwable th) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                    throw th;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            i -= 10;
            if (file.length() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID <= 512) {
                return;
            }
        } while (i > 0);
    }

    private void returnPhotoResult() {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        this.idCardBitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        Intent intent = new Intent();
        intent.putExtra("id_card_bitmap", byteArray);
        setResult(-1, intent);
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        if (i == 10) {
            if (allPermissionsGranted()) {
                startCamera();
            } else {
                Toast.makeText(this, "Permission not granted, unable to use camera", 0).show();
                finish();
            }
        }
    }

    private boolean allPermissionsGranted() {
        for (String str : REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(this, str) != 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.cameraExecutor.shutdown();
    }
}