414_删除二维码外面多余白边





删除二维码外面多余白边




    //生成二维码bitmap
    public static Bitmap getQrCodeImage(int bitmapWidth, int bitmapHeight, String text) throws WriterException {


        //创建Hashtable
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();


        //设置编码
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");


        //根据text创建BitMatrix
        BitMatrix matrix1 = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, bitmapWidth, bitmapHeight);
        BitMatrix matrix = deleteWhite(matrix1);


        //根据宽高创建像素数组
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];


        //依次给上黑点和白点
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = BLACK;
                }else{
                    pixels[y * width + x] = WHITE;
                }
            }
        }


        //创建bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);


        //设置像素点
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);


        return bitmap;
    }


    private static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;


        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++) {
            for (int j = 0; j < resHeight; j++) {
                if (matrix.get(i + rec[0], j + rec[1]))
                    resMatrix.set(i, j);
            }
        }
        return resMatrix;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值