天天看點

自定義二維碼掃描

自定義二維碼掃描

第一步:添加

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

第二步:

new IntentIntegrator(AccountLoginActivity.this)
                .setCaptureActivity(CaptureActivity.class)
                .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)// 掃碼的類型,可選:一維碼,二維碼,一/二維碼
                .setCameraId(0)// 選擇攝像頭,可使用前置或者後置
                .setPrompt("")
                .initiateScan();// 初始
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (result != null) {
                if (result.getContents() != null) {
                    String code = result.getContents();
                    Toast.makeText(this, "code " + code, Toast.LENGTH_SHORT).show();
                    Logger.d(Logger._JN, "onActivityResult download link:  :%s ", code);
                }
            }
        }

    }
           

完成。

哈哈哈,還有更細緻的自定義

CaptureActivity.java

package iptv.com.quick.module.login;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import butterknife.BindView;
import iptv.com.quick.R;
import iptv.com.quick.module.base.BaseActivity;


/**
 * @author zhou.jn
 * @date 2019/3/26
 * @describe 掃描二維碼Activity
 */

public class CaptureActivity extends BaseActivity {
    @BindView(R.id.barcodeView)
    DecoratedBarcodeView decoratedBarcodeView;
    @BindView(R.id.back_tv)
    TextView btn_back;
    private CaptureManager captureManager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_capture;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        captureManager = new CaptureManager(this, decoratedBarcodeView);
        captureManager.initializeFromIntent(getIntent(), savedInstanceState);
        captureManager.decode();
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        captureManager.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String permissions[],  int[] grantResults) {
        captureManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return decoratedBarcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
}

           

activity_capture.xml

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

    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/barcodeView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:zxing_framing_rect_height="@dimen/_412px_in720p"
        app:zxing_framing_rect_width="@dimen/_412px_in720p"
        app:zxing_preview_scaling_strategy="fitXY"
        app:zxing_scanner_layout="@layout/barcode_scanner"
        app:zxing_use_texture_view="false">

        <include layout="@layout/include_back_bar"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_60px_in720p"
            android:layout_marginTop="@dimen/_152px_in720p"
            android:text="@string/login_by_qr_code"
            android:textColor="@color/color_white"
            android:textSize="@dimen/_60px_in720p"
            android:textStyle="bold"
            />

        <TextView
            android:layout_width="@dimen/_500px_in720p"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_60px_in720p"
            android:layout_marginTop="@dimen/_250px_in720p"
            android:text="@string/scan_the_qr_code_in_the_supertv_tv_profile_and_log_in"
            android:textColor="@color/color_white"
            android:textSize="@dimen/_28px_in720p"
            />
    </com.journeyapps.barcodescanner.DecoratedBarcodeView>
</android.support.constraint.ConstraintLayout>
           

== 注意==:要注意DecoratedBarcodeView這個view,它是可以添加自定義的掃描view的,這樣,我們就有了自定義二維碼掃描及提示語。

include_back_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/back_tv"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/_22px_in720p"
    android:drawableLeft="@mipmap/back"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/_30px_in720p"
    android:text="@string/back"
    android:textColor="@color/color_white"
    android:textSize="@dimen/_28px_in720p"
    android:textStyle="bold"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>
           

barcode_scanner.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <com.journeyapps.barcodescanner.BarcodeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/zxing_barcode_surface"/>

    <iptv.com.quick.widget.CustomViewfinderView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/zxing_viewfinder_view"/>

    <TextView android:id="@+id/zxing_status_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom|center_horizontal"
              android:background="@color/zxing_transparent"
              android:text="@string/zxing_msg_default_status"
              android:textColor="@color/zxing_status_text"/>
</merge>

           

CustomViewfinderView.java

package iptv.com.quick.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;

import com.journeyapps.barcodescanner.ViewfinderView;

/**
 * 自定義zxing二維碼掃描界面
 * Created by IBM on 2016/10/20.
 */

public class CustomViewfinderView extends ViewfinderView {
    public int laserLinePosition = 0;
    public float[] position = new float[]{0f, 1f};
    //    public int[] colors=new int[]{0x00ffffff,0xffffffff,0x00ffffff};
    public int[] colors = new int[]{Color.parseColor("#00000000"), Color.parseColor("#FFC300")};
    public LinearGradient linearGradient;

    public CustomViewfinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    /**
     * 重寫draw方法繪制自己的掃描框
     *
     * @param canvas
     */
    @Override
    public void onDraw(Canvas canvas) {
        refreshSizes();
        if (framingRect == null || previewFramingRect == null) {
            return;
        }

        Rect frame = framingRect;
        Rect previewFrame = previewFramingRect;

        int width = canvas.getWidth();
        int height = canvas.getHeight();
        // Draw the exterior (i.e. outside the framing rect) darkened
        paint.setColor(resultBitmap != null ? resultColor : maskColor);
        canvas.drawRect(0, 0, width, frame.top, paint);
        canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
        canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
        canvas.drawRect(0, frame.bottom + 1, 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 {
            //  paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
            //  scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
            int middle = frame.height() / 2 + frame.top;
            laserLinePosition = laserLinePosition + 5;
            if (laserLinePosition > frame.height()) {
                laserLinePosition = 0;
            }
            linearGradient = new LinearGradient(frame.left, frame.top, frame.left, frame.top + 10 + laserLinePosition, colors, position, Shader.TileMode.CLAMP);
            paint.setShader(linearGradient);
            //繪制掃描線
            canvas.drawRect(frame.left + 1, frame.top, 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();

            int frameLeft = frame.left;
            int frameTop = frame.top;

            //繪制4個角
            paint.setColor(Color.parseColor("#FFC300"));
            paint.setStyle(Paint.Style.FILL);
            //        paint.setColor(0xFFFFFFFF);//定義畫筆的顔色
            int gap = 5;
            int borderWidth = 5;
            int borderHeight = 65;
            canvas.drawRect(frame.left - gap, frame.top - gap, frame.left + borderHeight, frame.top + borderWidth, paint);
            canvas.drawRect(frame.left - gap, frame.top - gap, frame.left + borderWidth, frame.top + borderHeight, paint);

            canvas.drawRect(frame.right - borderHeight, frame.top - gap, frame.right + gap, frame.top + borderWidth, paint);
            canvas.drawRect(frame.right - borderWidth, frame.top - gap, frame.right + gap, frame.top + borderHeight, paint);

            canvas.drawRect(frame.left - gap, frame.bottom - borderWidth, frame.left + borderHeight, frame.bottom + gap, paint);
            canvas.drawRect(frame.left - gap, frame.bottom - borderHeight, frame.left + borderWidth, frame.bottom + gap, paint);

            canvas.drawRect(frame.right - borderHeight, frame.bottom - borderWidth, frame.right + gap, frame.bottom + gap, paint);
            canvas.drawRect(frame.right - borderWidth, frame.bottom - borderHeight, frame.right + gap, frame.bottom + gap, paint);

            postInvalidateDelayed(16,
                    frame.left,
                    frame.top,
                    frame.right,
                    frame.bottom);
            // postInvalidate();

        }
    }
}

           

繼續閱讀