首页 > 其他分享 >安卓 Zxing扫码

安卓 Zxing扫码

时间:2023-11-17 18:55:34浏览次数:27  
标签:扫码 int 安卓 Zxing result import android com void

1. 添加依赖:

api 'com.google.zxing:core:3.3.3'
implementation 'com.king.zxing:zxing-lite:1.1.2-androidx'

 

2. xml布局文件:扫码界面

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <com.king.zxing.ViewfinderView
        android:id="@+id/viewfinderView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:labelTextColor="#ffffff"
        app:labelTextLocation="bottom"
        app:laserColor="#e06c51"
        app:frameColor="#e06c51"
        app:cornerColor="#e06c51"
        app:maskColor="#7f7f7f"
        app:laserStyle="line"
        app:gridHeight="0dp"/>

    <RelativeLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#000000"
        tools:ignore="MissingConstraints">

        <ImageView
            android:id="@+id/iv_scan_back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_gravity="center_vertical"
            android:paddingRight="14dp"
            android:src="@drawable/ic_back_dark_selector" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:ellipsize="middle"
            android:gravity="center"
            android:singleLine="true"
            android:text="@string/qrcode_scan_title"
            android:textColor="#ffffff"
            android:textSize="18dp" />

    </RelativeLayout>
    <TextView
        android:id="@+id/ivFlash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="560dp"
        android:visibility="gone"
        android:textColor="?android:attr/textColorPrimary"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

3.扫码页面的类:QRCaptureActivity.java

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Rect;
import android.hardware.Camera;
import android.media.Image;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.eshare.businessclient.FindDeviceActivity;
import com.eshare.businessclient.QRCodeActivity;
import com.king.zxing.CaptureActivity;
import com.king.zxing.DecodeFormatManager;
import com.king.zxing.OnCaptureCallback;
import com.king.zxing.camera.CameraConfigurationUtils;
import com.king.zxing.camera.open.CameraFacing;
import com.king.zxing.camera.open.OpenCamera;
import com.king.zxing.camera.open.OpenCameraInterface;
import com.viewsonic.vcastsender.R;

import java.io.IOException;

public class QRCaptureActivity extends CaptureActivity implements SurfaceHolder.Callback, OnCaptureCallback {
    private TextView ivFlash;
    private RelativeLayout ll;
    private ImageView ivScanBack;
    private SurfaceView surfaceView;
    int mSceenWidth;
    int mSceenHeight;

    public static void setMargins(View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_qrcapture;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("LXP","onCreate...");
        ll = findViewById(R.id.ll);
        setMargins(ll, 0, 50, 0, 0);
        TextView tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setTextColor(Color.WHITE);
        ivFlash = findViewById(R.id.ivFlash);
        if (!hasTorch()) {
            ivFlash.setVisibility(View.GONE);
        }

        ivScanBack = findViewById(R.id.iv_scan_back);
        ivScanBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        // 2.设置相机预览
        surfaceView = findViewById(R.id.surfaceView);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);

        //3.获取CaptureHelper,里面有扫码相关的配置设置
        getCaptureHelper().playBeep(false)//播放音效
                .vibrate(true)//震动
                .decodeFormats(DecodeFormatManager.QR_CODE_FORMATS)//设置只识别二维码会提升速度
                .supportVerticalCode(false)//支持扫垂直条码,建议有此需求时才使用。
                .continuousScan(false);//是否连扫
    }

    /**
     * 开启或关闭闪光灯(手电筒)
     *
     * @param on {@code true}表示开启,{@code false}表示关闭
     */
    public void setTorch(boolean on) {
        Camera camera = getCameraManager().getOpenCamera().getCamera();
        Camera.Parameters parameters = camera.getParameters();
        CameraConfigurationUtils.setTorch(parameters, on);
        camera.setParameters(parameters);
    }

    /**
     * 检测是否支持闪光灯(手电筒)
     *
     * @return
     */
    public boolean hasTorch() {
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }


    /**
     * 扫码结果回调
     *
     * @param result 扫码结果
     * @return
     */
    @Override
    public boolean onResultCallback(String result) {
        Log.d("LXP","onResultCallback ----- result:"+result);
        //获取到扫码结果HmsScan,判断结果是否有效
        if (result != null && !TextUtils.isEmpty(result)) {
            //成功
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("result_type", 1);
            bundle.putString("result_string", result);
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        } else {
            //失败
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("result_type", 2);
            bundle.putString("result_string", "");
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
        return super.onResultCallback(result);
    }


    private void clickFlash(View v) {
        boolean isSelected = v.isSelected();
        setTorch(!isSelected);
        v.setSelected(!isSelected);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivFlash:
                clickFlash(v);
                break;
        }
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
    }
}

4. 回调

1.点击按钮扫码
case R.id.tv_pincode_scan:
                if(PermissionUtils.requestCamera1Permissions(this)){
                    toQRCaptureActivity();
                }

2.跳转扫码页面
private void toQRCaptureActivity() {
        Log.d("LXP","scan qrcode");
        Intent intent1 = new Intent();
        intent1.putExtra(KEY_TITLE, this.getString(R.string.find_device));
        intent1.setClass(this.getApplicationContext(), QRCaptureActivity.class);
        startActivityForResult(intent1, REQUEST_CODE_SCAN);
    }
    
    
    
3.数据回调

  @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.d("LXP","onActivityResult-----requestCode:"+requestCode+" resultCode:"+resultCode);
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == -1) {
            String qrResult = data.getStringExtra("result_string");
            Log.d("LXP","qrResult:"+qrResult);
            if (!TextUtils.isEmpty(qrResult)) {
                qrResult = FileUtils.toURLDecoder(qrResult);
                parsingScanCodeData(qrResult);
            }
        }
    }

 

标签:扫码,int,安卓,Zxing,result,import,android,com,void
From: https://www.cnblogs.com/lxpblogs/p/17839475.html

相关文章

  • MT6785(Helio G95)核心板_MTK联发科安卓主板开发板
    联发科HelioG95(曦力G95)采用台积电12nmFinFET制程工艺,2*A76+6*A55架构,搭载Android9.0操作系统,主频最高达2.05GHz,搭载HyperEngine游戏技术,通过四个增强领域的整体增强功能。该处理器搭载ArmMali-G76MC4GPU运行速度可提升至900MHz。支持H.264、H.265/HEVC格式视频编码......
  • 闲鱼捡漏扫货脚本,低价下单,全自动安卓按键精灵开源代码!
    之前给客户定制的,功能就是可以在最新发布区识别低价商品,符合价格条件的自动下单这么一个效果,省的人工一个个去看价格。UI界面:  UI界面代码:=====================================================界面1:{激活页面(检测速度不建议快):{输入框:{名称:"输入框11",提示内......
  • Genio500_联发科MT8385安卓核心板参数介绍
    Genio500(MT8385)核心板是一款复杂但高效的AIoT平台核心板,这款设备是专为移动设备、家庭媒体和商业IoT应用而制作的。一些主要特色包括即时的边缘计算、出色的多媒体特性、高清晰度摄像头系统、联动的触屏设备以及强大的多任务运行系统。此外,此核心板的AI处理器(APU)工作达到500......
  • 信用卡审核通过生成软件,易安卓开发,用户输入信息自动绘图
    闲着没事干,用e4a给大家开发了一个审核通过的截图生成器,原理很简单,就插入了两个窗口,一个窗口是编辑区域,用户输入信息,一个按钮,另一个窗口是画板区域,画板里面插入了审核通过的图片,图片上面对应了指定的标签,标签客户通过窗口1的编辑框通过事件代码对应改变,比如:标签7.标题=读取设置("转......
  • 关于鸿蒙与安卓硬分叉的一点个人看法
    鸿蒙将与安卓应用形成“硬分叉”,多家平台急聘鸿蒙开发员|app_网易订阅(163.com)至少说明了一个事,之前鼓吹的自主研发,证实了是基于安卓的。跟小米,OV等魔改安卓差不多。所以它不是“兼容安卓应用”,而是从底层来看它就是AOSP,应该是能“兼容鸿蒙格式的应用"还差不多。另外这次所谓......
  • cq9玩家异想天开:苹果官网购iPhone 15竟然变身电子游戏的安卓山寨机!
    网购惊现苹果官网iPhone假货事件!英国网友Ed在Reddit上分享了他的离奇经历,从苹果官网购买的iPhone15ProMax竟然变成了一部Android山寨手机。外包装与正品无异,然而打开后发现手机下巴异常宽,原来安装的是伪装的Android系统。Ed强调他是从苹果官网购买,并提供了订单和出货确认信,使用D......
  • (原创)安卓快速使用简单的ViewPager
    原创声明:本文所有图片和代码皆由本人制作和编写。目录前言目标与效果4步走第一:在布局文件添加ViewPager组件第二:为ViewPager设计布局第三:创建适配器类第四:把布局文件和适配器跟主活动联系起来后记前言这学期刚开的安卓课程,为了写实验上网查资料,只想找简明扼要的教程来让我快速......
  • Genio 700/MT8390安卓核心板规格参数_MTK核心板定制
    联发科Genio700(MT8390)是一款针对智能家居、互动零售、工业和商业应用的高性能边缘人工智能物联网平台。它具有高度响应的边缘处理、先进的多媒体功能、多种传感器和连接选项,并支持多任务操作系统。4TOPSAPU性能:该高效的芯片内人工智能多处理器(APU)提供4TOPS的性能,......
  • Android安卓 match_parent与match_parent区别
    Android安卓match_parent与match_parent区别 在Android中,match_parent和fill_parent是布局参数(layoutparameters)中的两个常用属性,它们在XML中用于定义一个视图(View)的尺寸。在最新的Android版本中,fill_parent已经被废弃,而match_parent用于替代。**match_parent:**这个属......
  • Android安卓gravity和layout_gravity的区别
    在Android中,gravity和layout_gravity是用于控制视图元素在布局中位置的两个属性,它们有不同的作用和适用范围。gravity:gravity是用于设置视图元素内部内容(文本、图像等)在视图本身内的对齐方式。它影响的是视图内部内容的位置。<TextViewandroid:layout_width="wrap_conte......