Files
ZXingLite/lib/src/main/java/com/king/zxing/CaptureFragment.java
2020-12-24 17:50:42 +08:00

238 lines
6.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Copyright (C) 2019 Jenly Yu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.king.zxing;
import android.Manifest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.zxing.Result;
import com.king.zxing.util.LogUtils;
import com.king.zxing.util.PermissionUtils;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.camera.view.PreviewView;
import androidx.fragment.app.Fragment;
/**
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class CaptureFragment extends Fragment implements CameraScan.OnScanResultCallback {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 0X86;
private View mRootView;
protected PreviewView previewView;
protected ViewfinderView viewfinderView;
protected View ivFlashlight;
private CameraScan mCameraScan;
public static CaptureFragment newInstance() {
Bundle args = new Bundle();
CaptureFragment fragment = new CaptureFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int layoutId = getLayoutId();
if(isContentView(layoutId)){
mRootView = createRootView(inflater,container);
}
initUI();
return mRootView;
}
/**
* 初始化
*/
public void initUI(){
previewView = mRootView.findViewById(getPreviewViewId());
int viewfinderViewId = getViewfinderViewId();
if(viewfinderViewId != 0){
viewfinderView = mRootView.findViewById(viewfinderViewId);
}
int ivFlashlightId = getFlashlightId();
if(ivFlashlightId != 0){
ivFlashlight = mRootView.findViewById(ivFlashlightId);
if(ivFlashlight != null){
ivFlashlight.setOnClickListener(v -> toggleTorchState());
}
}
initCameraScan();
startCamera();
}
/**
* 初始化CameraScan
*/
public void initCameraScan(){
mCameraScan = new DefaultCameraScan(this,previewView);
mCameraScan.setOnScanResultCallback(this);
}
/**
* 启动相机预览
*/
public void startCamera(){
if(mCameraScan != null){
if(PermissionUtils.checkPermission(getContext(), Manifest.permission.CAMERA)){
mCameraScan.startCamera();
}else{
LogUtils.d("checkPermissionResult != PERMISSION_GRANTED");
PermissionUtils.requestPermission(this,Manifest.permission.CAMERA,CAMERA_PERMISSION_REQUEST_CODE);
}
}
}
/**
* 释放相机
*/
private void releaseCamera(){
if(mCameraScan != null){
mCameraScan.release();
}
}
/**
* 切换闪光灯状态(开启/关闭)
*/
protected void toggleTorchState(){
if(mCameraScan != null){
boolean isTorch = mCameraScan.isTorchEnabled();
mCameraScan.enableTorch(!isTorch);
if(ivFlashlight != null){
ivFlashlight.setSelected(!isTorch);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == CAMERA_PERMISSION_REQUEST_CODE){
requestCameraPermissionResult(permissions,grantResults);
}
}
/**
* 请求Camera权限回调结果
* @param permissions
* @param grantResults
*/
public void requestCameraPermissionResult(@NonNull String[] permissions, @NonNull int[] grantResults){
if(PermissionUtils.requestPermissionsResult(Manifest.permission.CAMERA,permissions,grantResults)){
startCamera();
}else{
getActivity().finish();
}
}
@Override
public void onDestroy() {
releaseCamera();
super.onDestroy();
}
/**
* 返回true时会自动初始化{@link #createRootView(LayoutInflater, ViewGroup)}返回为false是需自己去初始化{@link #createRootView(LayoutInflater, ViewGroup)}
* @param layoutId
* @return 默认返回true
*/
public boolean isContentView(@LayoutRes int layoutId){
return true;
}
/**
* 创建{@link #mRootView}
* @param inflater
* @param container
* @return
*/
@NonNull
public View createRootView(LayoutInflater inflater, ViewGroup container){
return inflater.inflate(getLayoutId(),container,false);
}
/**
* 布局id
* @return
*/
public int getLayoutId(){
return R.layout.zxl_capture;
}
/**
* {@link #viewfinderView} 的 ID
* @return 默认返回{@code R.id.viewfinderView}, 如果不需要扫码框可以返回0
*/
public int getViewfinderViewId(){
return R.id.viewfinderView;
}
/**
* 预览界面{@link #previewView} 的ID
* @return
*/
public int getPreviewViewId(){
return R.id.previewView;
}
/**
* 获取 {@link #ivFlashlight} 的ID
* @return 默认返回{@code R.id.ivFlashlight}, 如果不需要手电筒按钮可以返回0
*/
public int getFlashlightId(){
return R.id.ivFlashlight;
}
/**
* Get {@link CameraScan}
* @return {@link #mCameraScan}
*/
public CameraScan getCameraScan(){
return mCameraScan;
}
/**
* 接收扫码结果回调
* @param result 扫码结果
* @return 返回true表示拦截将不自动执行后续逻辑为false表示不拦截默认不拦截
*/
@Override
public boolean onScanResultCallback(Result result) {
return false;
}
//--------------------------------------------
public View getRootView() {
return mRootView;
}
}