美文网首页
ZXing 生成二维码,使用Canvas优化二维码显示速度

ZXing 生成二维码,使用Canvas优化二维码显示速度

作者: 贼噶人 | 来源:发表于2019-08-14 10:18 被阅读0次
    package com.mytian.lgwstory.view;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.support.annotation.Nullable;
    import android.util.AttributeSet;
    import android.view.View;
    
    import com.google.zxing.common.BitArray;
    import com.google.zxing.common.BitMatrix;
    
    public class BitMatrixView extends View {
        private BitMatrix mBitMatrix;
    
        public BitMatrixView(Context context) {
            super(context);
        }
    
        public BitMatrixView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public BitMatrixView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        Paint paint;
    
        {
            paint = new Paint();
            paint.setColor(Color.BLACK);
            paint.setAntiAlias(false);
        }
    
        public void refresh(BitMatrix mBitMatrix) {
            this.mBitMatrix = mBitMatrix;
            invalidate();
        }
    
        @Override
        protected void onDraw(final Canvas canvas) {
            super.onDraw(canvas);
            if (null != mBitMatrix) {
                final float[] pts = new float[mBitMatrix.getWidth() * 2];
                final BitArray bitArray = new BitArray(mBitMatrix.getWidth());
                int i;
                canvas.scale(4, 4);
                for (int y = 0; y < mBitMatrix.getHeight(); ++y) {
                    i = 0;
                    mBitMatrix.getRow(y, bitArray);
                    for (int x = 0; x < bitArray.getSize(); ++x) {
                        if (bitArray.get(x)) {
                            pts[i++] = x;
                            pts[i++] = y;
                        }
                    }
                    if (i > 0) {
                        canvas.drawPoints(pts, 0, i + 1, paint);
                    }
                }
                canvas.scale(1, 1);
            }
        }
    }
    
    

    使用

     doPost(subscription = Observable.just(content).subscribeOn(Schedulers.computation()).map
                    (new Func1<String, BitMatrix>() {
                @Override
                public BitMatrix call(String content) {
                    try {
                        return new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,
                                mBitCodeImageView.getWidth() / 4, mBitCodeImageView.getHeight() / 4,
                                hints);
                    } catch (WriterException e) {
                        return null;
                    }
                }
            }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<BitMatrix>() {
                @Override
                public void call(final BitMatrix matrix) {
                    mBitCodeImageView.refresh(matrix);
                    hideLoading();
                    logo.setVisibility(View.VISIBLE);
                }
            }));
    

    相关文章

      网友评论

          本文标题:ZXing 生成二维码,使用Canvas优化二维码显示速度

          本文链接:https://www.haomeiwen.com/subject/enwwjctx.html