mac
2024-06-25 7562541202056446945028026f1cbc64bb2407db
app/src/main/java/com/hdl/photovoltaic/ui/device/ScanActivity.java
File was renamed from app/src/main/java/com/hdl/photovoltaic/ui/device/FastScanActivity.java
@@ -1,29 +1,36 @@
package com.hdl.photovoltaic.ui.device;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Rect;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.content.res.AppCompatResources;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
@@ -35,9 +42,11 @@
import com.google.zxing.qrcode.QRCodeReader;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.base.CustomBaseActivity;
import com.hdl.photovoltaic.databinding.ActivityFastScanBinding;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.other.HdlCommonLogic;
import com.hdl.photovoltaic.uni.HDLUniMP;
import com.hdl.photovoltaic.utils.LocalManageUtil;
import com.jaeger.library.StatusBarUtil;
import com.zxing.IZxingActivity;
import com.zxing.camera.CameraManager;
import com.zxing.decode.DecodeThread;
@@ -50,9 +59,13 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Hashtable;
import java.util.Locale;
public class FastScanActivity extends CustomBaseActivity implements IZxingActivity, SurfaceHolder.Callback {
    private static final String TAG = "FastScanActivity";
/**
 * 扫一扫
 */
public class ScanActivity extends Activity implements IZxingActivity, SurfaceHolder.Callback {
    private static final String TAG = ScanActivity.class.getSimpleName();
    private final int REQUEST_CODE = 33;
    private CameraManager cameraManager;
    private CaptureActivityHandler handler;
@@ -61,9 +74,10 @@
    private SurfaceView scanPreview = null;
    private RelativeLayout scanContainer;
    private RelativeLayout scanCropView;
    private TextView tvCancel;
    private Rect mCropRect = null;
    private boolean isHasSurface = false;
    private ActivityFastScanBinding viewBinding;
    @Override
    public Handler getHandler() {
@@ -80,34 +94,51 @@
        return cameraManager;
    }
    @Override
    public Object getContentView() {
        viewBinding = ActivityFastScanBinding.inflate(getLayoutInflater());
        return viewBinding.getRoot();
    public int getRootLayoutId() {
        return R.layout.activity_scan;
    }
    @Override
    public void onBindView(Bundle savedInstanceState) {
        setStatusBarTranslucent();
//        Window window = getWindow();
//        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        //设置根视图
//        View mContentView = LayoutInflater.from(this).inflate(R.layout.activity_new_capture, null);
//        setContentView(mContentView);
        //初始化
        initView(savedInstanceState);
        //初始化界面监听器
        initEvent();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Resources resources = this.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = LocalManageUtil.getLocale(UserConfigManage.getInstance().getCurrentAppLanguage());
        // app locale 默认简体中文
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
        StatusBarUtil.setTranslucentForImageViewInFragment(this,
                0, null);
        StatusBarUtil.setLightMode(this);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //设置根视图
        View mContentView = LayoutInflater.from(this).inflate(getRootLayoutId(), null);
        setContentView(mContentView);
        afterViewBind(mContentView, savedInstanceState);
    }
    private void initView(Bundle savedInstanceState) {
        scanPreview = findViewById(R.id.new_capture_preview);
        scanContainer = findViewById(R.id.new_capture_rl);
        scanCropView = findViewById(R.id.new_capture_crop_view);
        ImageView scanLine = findViewById(R.id.new_capture_scan_line);
    public void afterViewBind(View rootView, Bundle savedInstanceState) {
        tvCancel = findViewById(R.id.tv_text_cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
//                enterGallery();
            }
        });
        scanPreview = findViewById(R.id.capture_preview);
        scanContainer = findViewById(R.id.capture_container);
        scanCropView = findViewById(R.id.capture_crop_view);
        ImageView scanLine = findViewById(R.id.capture_scan_line);
        inactivityTimer = new InactivityTimer(this);
        beepManager = new BeepManager(this);
@@ -118,27 +149,16 @@
        animation.setRepeatCount(-1);
        animation.setRepeatMode(Animation.RESTART);
        scanLine.startAnimation(animation);
    }
    private void initEvent() {
        viewBinding.newTopBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewBinding.newLightIv.setOnClickListener(new View.OnClickListener() {
        ImageView imageView = findViewById(R.id.light_iv);
        //开启闪光灯按钮事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                if (v.isSelected()) {
                    viewBinding.newLightIv.setImageDrawable(AppCompatResources.getDrawable(FastScanActivity.this, com.zxing.R.drawable.fast_scan_light_open));
                    imageView.setImageDrawable(AppCompatResources.getDrawable(ScanActivity.this, com.zxing.R.drawable.fast_scan_light_open));
                } else {
                    viewBinding.newLightIv.setImageDrawable(AppCompatResources.getDrawable(FastScanActivity.this, com.zxing.R.drawable.fast_scan_light_close));
                    imageView.setImageDrawable(AppCompatResources.getDrawable(ScanActivity.this, com.zxing.R.drawable.fast_scan_light_close));
                }
                flashLightOnOrOff();
@@ -146,14 +166,13 @@
        });
    }
    public void enterGallery() {
        // 进入图库
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE);
        Intent pickIntent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//启动系统相册
        pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(pickIntent, REQUEST_CODE);
    }
    @Override
@@ -246,11 +265,11 @@
            initCrop();
        } catch (IOException ioe) {
            Log.w(TAG, ioe);
            Toast.makeText(this, com.zxing.R.string.capture_no_camera, Toast.LENGTH_SHORT).show();
            Toast.makeText(this, R.string.capture_no_camera, Toast.LENGTH_SHORT).show();
            finish();
        } catch (RuntimeException e) {
            Log.w(TAG, "Unexpected error initializing camera", e);
            Toast.makeText(this, com.zxing.R.string.capture_no_camera, Toast.LENGTH_SHORT).show();
            Toast.makeText(this, R.string.capture_no_camera, Toast.LENGTH_SHORT).show();
            finish();
        }
    }
@@ -315,25 +334,24 @@
    }
    private void doProcess(String result) {
        Log.d("panlili", "scanResult: " + result);
        /*if (!DeviceHelper.getNetworkState()) {
            Toast.makeText(this, R.string.capture_no_network, Toast.LENGTH_SHORT).show();
            return;
        }*/
        if (TextUtils.isEmpty(result)) {
            Toast.makeText(this, com.zxing.R.string.capture_no_result, Toast.LENGTH_SHORT).show();
            Toast.makeText(this, R.string.capture_no_result, Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent();
            intent.putExtra("data", result);
            setResult(RESULT_OK, intent);
            finish();
            //EventBus事件分发
            HdlCommonLogic.getInstance().postEventBusSticky(
                    HDLUniMP.UNI_EVENT_REPLY_HOME_ADD,
                    HDLUniMP.UNI_EVENT_REPLY_HOME_ADD,
                    result);
        }
    }
@@ -356,7 +374,7 @@
                if (!TextUtils.isEmpty(path)) {
                    handleQRCodeFormPhoto(path);
                } else {
                    Toast.makeText(this, "图片已损坏,请重新选择!", Toast.LENGTH_SHORT).show();
//                    Toast.makeText(this, R.string.pic_error, Toast.LENGTH_SHORT).show();
                }
            }
        }
@@ -401,14 +419,13 @@
            @Override
            public void run() {
                if (progressInfo == null) {
                    Toast.makeText(FastScanActivity.this, com.zxing.R.string.capture_no_result2, Toast.LENGTH_SHORT).show();
                    Toast.makeText(ScanActivity.this, R.string.capture_no_result2, Toast.LENGTH_SHORT).show();
                } else {
                    doProcess(progressInfo.toString());
                }
            }
        });
    }
    /**
     * 闪光灯开启或者关闭
@@ -437,6 +454,5 @@
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }
}