最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)掃碼功能

 更新時(shí)間:2021年05月31日 16:28:31   作者:baidu_24743861  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)掃碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)掃碼功能的具體代碼,供大家參考,具體內(nèi)容如下

1、引入

implementation 'com.journeyapps:zxing-android-embedded:3.5.0'

2、使用:

public void initScan() {
        IntentIntegrator integrator = new IntentIntegrator(this);
        // 設(shè)置要掃描的條碼類型,ONE_D_CODE_TYPES:一維碼,QR_CODE_TYPES-二維碼
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        integrator.setCaptureActivity(ScanActivity.class); //設(shè)置打開(kāi)攝像頭的Activity
        integrator.setPrompt(""); //底部的提示文字,設(shè)為""可以置空
        integrator.setCameraId(0); //前置或者后置攝像頭
        integrator.setBeepEnabled(true); //掃描成功的「嗶嗶」聲,默認(rèn)開(kāi)啟
        integrator.setBarcodeImageEnabled(true);
        integrator.initiateScan();
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == IntentIntegrator.REQUEST_CODE) {
            IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
            if (scanResult != null && scanResult.getContents() != null) {
                String result = scanResult.getContents();
                LogUtil.d("掃碼返回: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.has(Constant.USERPOLICEMENID)) {
                       //TODO  邏輯
                     
                    } else {
                        ToastUtil.showShortToast("未掃描到有效的信息");
                    }
                } catch (Exception e) {
                    ToastUtil.showShortToast("未掃描到有效的信息");
                    e.printStackTrace();
                }
            } else {
                ToastUtil.showShortToast("未掃描到有效的信息");
            }
        }
    }

3、ScanActivity

public class ScanActivity extends BackActivity {
 
    @BindView(R.id.dbv)
    DecoratedBarcodeView mDbv;
    private CaptureManager captureManager;
 
    @Override
    protected int getLayoutId() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_scan;
    }
 
    @Override
    protected void init() {
        super.init();
        captureManager = new CaptureManager(this, mDbv);
        captureManager.initializeFromIntent(getIntent(), getSavedInstanceState());
        captureManager.decode();
    }
 
    @Override
    public void onSaveInstanceState(@NotNull Bundle outState, @NotNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        captureManager.onSaveInstanceState(outState);
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mDbv.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }
 
    @Override
    public void onResume() {
        super.onResume();
        captureManager.onResume();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }
 
}

4、布局文件

activity_scan

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ui.main.alarmassistant.ScanActivity">
 
 
    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/dbv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:zxing_framing_rect_height="200dp"
        app:zxing_framing_rect_width="200dp"
        app:zxing_preview_scaling_strategy="fitXY"
        app:zxing_scanner_layout="@layout/view_qr"
        app:zxing_use_texture_view="true" />
</LinearLayout>

view_qr

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
 
    <com.journeyapps.barcodescanner.BarcodeView
        android:id="@+id/zxing_barcode_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:zxing_framing_rect_height="50dp"
        app:zxing_framing_rect_width="250dp" />
 
    <com.x.x.widget.QrView
        android:id="@+id/zxing_viewfinder_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:zxing_possible_result_points="@color/color_white"
        app:zxing_result_view="@color/zxing_custom_result_view"
        app:zxing_viewfinder_laser="@color/color_white"
        app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" />
 
    <TextView
        android:id="@+id/zxing_status_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="30dp"
        android:background="@color/zxing_transparent"
        android:text="@string/zxing_msg_default_status"
        android:textColor="@color/zxing_status_text" />
 
    <com.x.common.widget.MyActionBar
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:dark_mode="true"
        app:title="掃一掃" />
</merge>

5、

/**
 * 自定義掃描界面
 */
 
public class QrView extends ViewfinderView {
 
    public int laserLinePosition = 0;
    public float[] position = new float[]{0f, 0.5f, 1f};
    public int[] colors = new int[]{0x0027B14D, 0xff27B14D, 0x0027B14D};
    public LinearGradient linearGradient;
    private int ScreenRate;
 
    public QrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        float density = context.getResources().getDisplayMetrics().density;
        ScreenRate = (int) (15 * density);
    }
 
    @Override
    public void onDraw(Canvas canvas) {
        int CORNER_WIDTH = 15;
        refreshSizes();
 
        if (framingRect == null || previewFramingRect == null) {
            return;
        }
 
        Rect frame = framingRect;
        Rect previewFrame = previewFramingRect;
 
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        //繪制4個(gè)角
 
        paint.setColor(getResources().getColor(R.color.color_white));//定義畫(huà)筆的顏色
        canvas.drawRect(frame.left, frame.top, frame.left + ScreenRate, frame.top + CORNER_WIDTH, paint);
        canvas.drawRect(frame.left, frame.top, frame.left + CORNER_WIDTH, frame.top + ScreenRate, paint);
 
        canvas.drawRect(frame.right - ScreenRate, frame.top, frame.right, frame.top + CORNER_WIDTH, paint);
        canvas.drawRect(frame.right - CORNER_WIDTH, frame.top, frame.right, frame.top + ScreenRate, paint);
 
        canvas.drawRect(frame.left, frame.bottom - CORNER_WIDTH, frame.left + ScreenRate, frame.bottom, paint);
        canvas.drawRect(frame.left, frame.bottom - ScreenRate, frame.left + CORNER_WIDTH, frame.bottom, paint);
 
        canvas.drawRect(frame.right - ScreenRate, frame.bottom - CORNER_WIDTH, frame.right, frame.bottom, paint);
        canvas.drawRect(frame.right - CORNER_WIDTH, frame.bottom - ScreenRate, frame.right, frame.bottom, paint);
 
        // 畫(huà)出外部(即構(gòu)圖矩形之外)變暗
        paint.setColor(resultBitmap != null ? resultColor : maskColor);
        canvas.drawRect(0, 0, width, frame.top, paint);
        canvas.drawRect(0, frame.top, frame.left, frame.bottom, paint);
        canvas.drawRect(frame.right, frame.top, width, frame.bottom, paint);
        canvas.drawRect(0, frame.bottom, width, height, paint);
 
        if (resultBitmap != null) {
            // Draw the opaque result bitmap over the scanning rectangle
            paint.setAlpha(CURRENT_POINT_OPACITY);
            canvas.drawBitmap(resultBitmap, null, frame, paint);
        } else {
 
            laserLinePosition = laserLinePosition + 8;
            if (laserLinePosition >= frame.height()) {
                laserLinePosition = 0;
            }
            linearGradient = new LinearGradient(frame.left + 1, frame.top + laserLinePosition, frame.right - 1, frame.top + 10 + laserLinePosition, colors, position, Shader.TileMode.CLAMP);
            // Draw a red "laser scanner" line through the middle to show decoding is active
 
            paint.setShader(linearGradient);
            //繪制掃描線
            canvas.drawRect(frame.left + 1, frame.top + laserLinePosition, frame.right - 1, frame.top + 10 + laserLinePosition, paint);
            paint.setShader(null);
            float scaleX = frame.width() / (float) previewFrame.width();
            float scaleY = frame.height() / (float) previewFrame.height();
 
            List<ResultPoint> currentPossible = possibleResultPoints;
            List<ResultPoint> currentLast = lastPossibleResultPoints;
            int frameLeft = frame.left;
            int frameTop = frame.top;
            if (currentPossible.isEmpty()) {
                lastPossibleResultPoints = null;
            } else {
                possibleResultPoints = new ArrayList<>(5);
                lastPossibleResultPoints = currentPossible;
                paint.setAlpha(CURRENT_POINT_OPACITY);
                paint.setColor(resultPointColor);
                for (ResultPoint point : currentPossible) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint);
                }
            }
            if (currentLast != null) {
                paint.setAlpha(CURRENT_POINT_OPACITY / 2);
                paint.setColor(resultPointColor);
                float radius = POINT_SIZE / 2.0f;
                for (ResultPoint point : currentLast) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX), frameTop + (int) (point.getY() * scaleY), radius, paint);
                }
            }
            postInvalidateDelayed(16, frame.left, frame.top, frame.right, frame.bottom);
        }
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android模擬器接收UDP數(shù)據(jù)包的若干問(wèn)題分析

    Android模擬器接收UDP數(shù)據(jù)包的若干問(wèn)題分析

    這篇文章主要介紹了Android模擬器接收UDP數(shù)據(jù)包的若干問(wèn)題,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android模擬器接收UDP數(shù)據(jù)的使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-04-04
  • Android用webView包裝WebAPP方法

    Android用webView包裝WebAPP方法

    本篇文章通過(guò)流程講解給大家詳細(xì)介紹了Android用webView包裝WebAPP的方法以及需要注意的地方,需要的朋友參考學(xué)習(xí)下。
    2018-02-02
  • Android獲取SDcard目錄及創(chuàng)建文件夾的方法

    Android獲取SDcard目錄及創(chuàng)建文件夾的方法

    今天小編就為大家分享一篇Android獲取SDcard目錄及創(chuàng)建文件夾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Flutter加載圖片流程之ImageProvider源碼示例解析

    Flutter加載圖片流程之ImageProvider源碼示例解析

    這篇文章主要為大家介紹了Flutter加載圖片流程之ImageProvider源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 一篇文章弄懂Android自定義viewgroup的相關(guān)難點(diǎn)

    一篇文章弄懂Android自定義viewgroup的相關(guān)難點(diǎn)

    這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章弄懂Android中自定義viewgroup的一些相關(guān)難點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • Android AES加密工具類分享

    Android AES加密工具類分享

    這篇文章主要介紹了Android AES加密工具類分享,本文給出了實(shí)現(xiàn)代碼和使用例子,本文使用PKCS5Padding加密方式實(shí)現(xiàn),需要的朋友可以參考下
    2014-10-10
  • Android實(shí)現(xiàn)界面的自動(dòng)跳轉(zhuǎn)功能

    Android實(shí)現(xiàn)界面的自動(dòng)跳轉(zhuǎn)功能

    界面自動(dòng)跳轉(zhuǎn)是指在應(yīng)用啟動(dòng)或某個(gè)特定界面顯示后,經(jīng)過(guò)預(yù)定的時(shí)間或者滿足某些條件后,自動(dòng)跳轉(zhuǎn)到另一個(gè)目標(biāo)界面,本文小編給大家講解了Android實(shí)現(xiàn)界面的自動(dòng)跳轉(zhuǎn)功能,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2025-04-04
  • Android編程開(kāi)發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)

    Android編程開(kāi)發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)

    這篇文章主要介紹了Android編程開(kāi)發(fā)之TextView文字顯示和修改方法,結(jié)合實(shí)例詳細(xì)分析了Android中TextView控件關(guān)于文字的顯示及修改技巧,并附帶了TextView屬性介紹,需要的朋友可以參考下
    2015-12-12
  • Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式的方法

    Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式的方法

    這篇文章主要介紹了Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。感興趣的小伙伴可以參考一下
    2022-06-06
  • Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例

    Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例

    這篇文章主要介紹了Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-08-08

最新評(píng)論

印江| 青冈县| 柳林县| 承德市| 斗六市| 阳原县| 汪清县| 连山| 鄂伦春自治旗| 剑阁县| 浠水县| 锡林浩特市| 增城市| 柘城县| 霍山县| 扬州市| 南川市| 牡丹江市| 洪江市| 延津县| 朝阳市| 丹阳市| 本溪市| 平江县| 雅江县| 佛学| 阜平县| 长宁县| 白河县| 阜新| 武安市| 旌德县| 浦江县| 绥德县| 姜堰市| 汝阳县| 崇仁县| 阿克陶县| 会泽县| 河南省| 马公市|