导航菜单

页面标题

页面副标题

St.John's v1.0.9 - MCBScannerActivity.java 源代码

正在查看: St.John's v1.0.9 应用的 MCBScannerActivity.java JAVA 源代码文件

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


package com.mcb.stjohnsemschool.activity;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;

public class MCBScannerActivity extends AppCompatActivity {
    private static final String TAG = "com.mcb.stjohnsemschool.activity.MCBScannerActivity";
    private ObjectAnimator animator;
    private SurfaceView cameraPriview;
    private CameraSource cameraSource;
    private View scannerBar;
    private RelativeLayout scannerLayout;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_qr_or_barcode_scan);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        init();
        this.cameraPriview = (SurfaceView) findViewById(R.id.surface);
        BarcodeDetector build = new BarcodeDetector.Builder(this).build();
        this.cameraSource = new CameraSource.Builder(this, build).setAutoFocusEnabled(true).setRequestedPreviewSize(1600, 1024).build();
        this.cameraPriview.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
            }

            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if (ActivityCompat.checkSelfPermission(MCBScannerActivity.this, "android.permission.CAMERA") != 0) {
                    return;
                }
                try {
                    MCBScannerActivity.this.cameraSource.start(MCBScannerActivity.this.cameraPriview.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                MCBScannerActivity.this.cameraSource.stop();
            }
        });
        build.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                SparseArray<Barcode> detectedItems = detections.getDetectedItems();
                if (detectedItems.size() > 0) {
                    Intent intent = new Intent();
                    intent.putExtra("ScanValue", detectedItems.valueAt(0));
                    intent.putExtra("scan_type", 0);
                    MCBScannerActivity.this.setResult(0, intent);
                    MCBScannerActivity.this.finish();
                }
            }
        });
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == 16908332) {
            finish();
        }
        return super.onOptionsItemSelected(menuItem);
    }

    private void init() {
        this.scannerLayout = (RelativeLayout) findViewById(R.id.scannerLayout);
        View findViewById = findViewById(R.id.scannerBar);
        this.scannerBar = findViewById;
        this.animator = null;
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) findViewById.getLayoutParams();
        layoutParams.setMargins(0, -280, 0, 0);
        this.scannerBar.setLayoutParams(layoutParams);
        startScannerAnimation();
    }

    private void startScannerAnimation() {
        this.scannerLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                MCBScannerActivity.this.scannerLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                MCBScannerActivity.this.scannerLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                float y = MCBScannerActivity.this.scannerLayout.getY() + MCBScannerActivity.this.scannerLayout.getHeight();
                MCBScannerActivity mCBScannerActivity = MCBScannerActivity.this;
                mCBScannerActivity.animator = ObjectAnimator.ofFloat(mCBScannerActivity.scannerBar, "translationY", MCBScannerActivity.this.scannerLayout.getY(), y);
                MCBScannerActivity.this.animator.setRepeatMode(2);
                MCBScannerActivity.this.animator.setRepeatCount(-1);
                MCBScannerActivity.this.animator.setInterpolator(new AccelerateDecelerateInterpolator());
                MCBScannerActivity.this.animator.setDuration(3000L);
                MCBScannerActivity.this.animator.start();
            }
        });
    }

    protected void onPause() {
        super.onPause();
    }

    protected void onStop() {
        super.onStop();
        this.cameraSource.stop();
    }

    public void onBackPressed() {
        super.onBackPressed();
        this.cameraSource.stop();
        finish();
    }
}