Androidstudio 扫码、生成二维码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40391500/article/details/87912095

扫码

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

IntentIntegrator integrator = new IntentIntegrator(MyApplication.activity);
// 设置要扫描的条码类型,ONE_D_CODE_TYPES:一维码,QR_CODE_TYPES-二维码
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setCaptureActivity(ScanActivity.class); //设置打开摄像头的Activity
integrator.setPrompt("扫描二维码加好友"); //底部的提示文字,设为""可以置空
integrator.setCameraId(0); //前置或者后置摄像头
integrator.setBeepEnabled(true); //扫描成功的滴声,默认开启
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null){
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanResult != null) {
            String result = scanResult.getContents();
            LogUtils.log("code", result);
        }
    }
}

//自定义扫描界面
public class ScanActivity extends BaseActivity {

    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scan);
        barcodeScannerView = findViewById(R.id.dbv_scan);

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();
    }

    /**
     * Override to use a different layout.
     *
     * @return the DecoratedBarcodeView
     */
    protected DecoratedBarcodeView initializeContent() {

        return (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
    }

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

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

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

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

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

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/dbv_scan"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="@dimen/sp20"
        android:text="@string/friend_refuse"/>
</LinearLayout>

二维码

	private final int width = 300;
    private final int height = 300;
	/**
     * 用于创建一个二维码
     * @param content
     * @param bm_icon 在二维码上添加一张图片
     */
    public Bitmap createQRCode(String content, ImageView iv_view, Bitmap bm_icon) {

        Bitmap bitmapCode = null;
        try {
            //创建实例化对象
            QRCodeWriter writer = new QRCodeWriter() ;
            //设置字符集
            Map<EncodeHintType, String> map = new HashMap<EncodeHintType, String>();
            map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            //设置空白边距
            map.put(EncodeHintType.MARGIN, "2");
            //通过encode方法将内容写入矩阵对象
            BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height,map);
            //定义一个二维码像素点的数组,向每个像素点中填充颜色
            int[] pixels = new int[width*height];
            //往每一像素点中填充颜色(像素没数据则用黑色填充,没有则用彩色填充,不过一般用白色)
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (matrix.get(j, i)) {
                        pixels[i*width+j] = 0xff000000;
                    }else {
                        pixels[i*width+j] = 0xffffffff;
                    }
                }
            }
            //创建一个指定高度和宽度的空白bitmap对象
            Bitmap bm_QR = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            //将每个像素的颜色填充到bitmap对象
            bm_QR.setPixels(pixels, 0, width, 0, 0, width, height);

            //创建一个方法在二维码上添加一张图片
            bitmapCode = addLogin(bm_QR,bm_icon);
            if (bitmapCode != null) {
                iv_view.setImageBitmap(bitmapCode);
                return bitmapCode;
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return bitmapCode;
    }


    /**
     * 用于向创建的二维码中添加一个logn
     * @param bm_QR
     * @param bm_login
     * @return
     */
    private Bitmap addLogin(Bitmap bm_QR, Bitmap bm_login) {
        if (bm_QR == null) {
            return null;
        }
        if (bm_login == null) {
            return bm_QR ;
        }

        //获取图片的宽高
        int bm_QR_Width = bm_QR.getWidth() ;
        int bm_QR_Height = bm_QR.getHeight();
        int bm_login_Width = bm_login.getWidth() ;
        int bm_login_Height = bm_login.getHeight();

        //设置logn的大小为二维码整体大小的1/7,图标太大扫描很久或扫不出来
        float scale_login = bm_QR_Width*1.0f /7/bm_login_Width ;
        Bitmap bitmap = Bitmap.createBitmap(bm_QR_Width, bm_QR_Height, Bitmap.Config.ARGB_8888);

        try {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(bm_QR, 0, 0, null);
            canvas.scale(scale_login, scale_login, bm_QR_Width / 2, bm_QR_Height / 2);
            canvas.drawBitmap(bm_login, (bm_QR_Width - bm_login_Width) / 2, (bm_QR_Height - bm_login_Height) / 2, null);

            canvas.save();
            canvas.restore();
        } catch (Exception e) {
            bitmap = null;
            e.getStackTrace();
        }
        return bitmap;
    }

猜你喜欢

转载自blog.csdn.net/weixin_40391500/article/details/87912095
0条评论
添加一条新回复