Java Code Examples for com.google.zxing.WriterException#printStackTrace()

The following examples show how to use com.google.zxing.WriterException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QrCodeUtils.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap textToBarCode(String data) {
    MultiFormatWriter writer = new MultiFormatWriter();


    String finaldata = Uri.encode(data, "utf-8");

    BitMatrix bm = null;
    try {
        bm = writer.encode(finaldata, BarcodeFormat.CODE_128, 150, 150);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = Bitmap.createBitmap(180, 40, Bitmap.Config.ARGB_8888);

    for (int i = 0; i < 180; i++) {//width
        for (int j = 0; j < 40; j++) {//height
            bitmap.setPixel(i, j, bm.get(i, j) ? BLACK : WHITE);
        }
    }

    return bitmap;
}
 
Example 2
Source File: QrCodeGenerator.java    From QrCodeLib with MIT License 6 votes vote down vote up
public static Bitmap getQrCodeImage(String data, int width, int height) {
    if (data == null || data.length() == 0) {
        return null;
    }
    Map<EncodeHintType, Object> hintsMap = new HashMap<>(3);
    hintsMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hintsMap.put(EncodeHintType.MARGIN, 0);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, height, hintsMap);
        Bitmap bitmap = bitMatrix2Bitmap(bitMatrix);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: QRCodeEngine.java    From SlidesRemote with Apache License 2.0 6 votes vote down vote up
public static Image encode(String data, int width, int height) {
    try {
        BitMatrix byteMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, width, height);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bufferedImage.createGraphics();
        Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
        graphics.setColor(Color.decode("#00adb5"));
        graphics.fillRect(0, 0, width, height);
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }

        return SwingFXUtils.toFXImage(bufferedImage, null);
    } catch (WriterException ex) {
        ex.printStackTrace();
        return null;
    }
}
 
Example 4
Source File: TokIdPresenter.java    From Tok-Android with GNU General Public License v3.0 5 votes vote down vote up
private String generateQR(String tokId) {
    String outPath = StorageUtil.getQrCodeFile();
    int qrCodeSize = 800;
    QRCodeEncode qrCodeEncoder =
        new QRCodeEncode(tokId, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(),
            qrCodeSize);
    try {
        ImageUtils.compressBitmap(qrCodeEncoder.encodeAsBitmap(), outPath);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return outPath;
}
 
Example 5
Source File: QRCodeView.java    From styT with Apache License 2.0 5 votes vote down vote up
public void createQRImage(String url) {
    int QR_WIDTH = mScreenWidth;
    int QR_HEIGHT = mScreenWidth;
    try {
        //判断URL合法性
        if (url == null || "".equals(url) || url.length() < 1) {
            return;
        }
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
        int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
        //下面这里按照二维码的算法,逐个生成二维码的图片,
        //两个for循环是图片横列扫描的结果
        for (int y = 0; y < QR_HEIGHT; y++) {
            for (int x = 0; x < QR_WIDTH; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * QR_WIDTH + x] = 0xff000000;
                } else {
                    pixels[y * QR_WIDTH + x] = 0xffffffff;
                }
            }
        }
        //生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
        //显示到一个ImageView上面
        ivQrcode.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: AddProductFragmentHandler.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
public Bitmap getBarCodeBitmap(String barCodeNumber) {
    String text = barCodeNumber; // Whatever you need to encode in the QR code
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        if (!text.isEmpty()) {
            BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.CODABAR, 380, 100);
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
            return bitmap;
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 7
Source File: AddProductFragment.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
public void setBarcode(String barCodeNumber) {
    String text = barCodeNumber + ""; // Whatever you need to encode in the QR code
    Log.d(ContentValues.TAG, "generateBarcode: " + text);
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.CODABAR, 380, 100);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        binding.barCode.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: AddProductFragment.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
public void generateBarcode(Product product) {
    String text = createRandomInteger(); // Whatever you need to encode in the QR code
    product.setBarCode(text);
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.CODABAR, 380, 100);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        binding.barCode.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: EncodingUtils.java    From imsdk-android with MIT License 5 votes vote down vote up
/**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000;
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 10
Source File: AbsCodec.java    From StarBarcode with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap encodeBarcode(String content, int widthPixel, int heightPixel) {

    if (TextUtils.isEmpty(content))
        throw new NullPointerException("The content of the QR code image cannot be empty");
    Map<EncodeHintType, Object> hintTypeMap = new EnumMap<>(EncodeHintType.class);
    hintTypeMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
    //空白边距的宽度
    hintTypeMap.put(EncodeHintType.MARGIN, 0);
    //容错级别
    hintTypeMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    try {
        //矩阵转换
        BitMatrix matrix = new MultiFormatWriter().encode(content, getEncodeFormat(), widthPixel, heightPixel, hintTypeMap);
        int[] pixels = new int[widthPixel * heightPixel];
        //使用二维码算法,逐个生成二维码的图片
        for (int y = 0; y < heightPixel; y++) {
            for (int x = 0; x < widthPixel; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * widthPixel + x] = 0xff000000; //前景色
                } else {
                    pixels[y * widthPixel + x] = 0xffffffff; //背景色
                }
            }
        }
        final Bitmap bitmap = Bitmap.createBitmap(widthPixel, heightPixel, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPixel, 0, 0, widthPixel, heightPixel);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 11
Source File: ZxingUtils.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 生成二维码图片
 *
 * @return
 */
public static Bitmap createBitmap(String text, int QR_WIDTH, int QR_HEIGHT) {
    Bitmap bitmap = null;
    try {
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new QRCodeWriter().encode(text,
                BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);

        int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
        for (int y = 0; y < QR_HEIGHT; y++) {
            for (int x = 0; x < QR_WIDTH; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * QR_WIDTH + x] = 0xff000000;
                } else {
                    pixels[y * QR_WIDTH + x] = 0xffffffff;
                }

            }
        }
        bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
                Bitmap.Config.ARGB_4444);
        bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);

    } catch (WriterException e) {
        e.printStackTrace();
    }

    return bitmap;
}
 
Example 12
Source File: EncodingUtils.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别 这里选择最高H级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000; // 黑色
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;// 白色
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {//绘制logo
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 13
Source File: QRPresenterActivity.java    From Meshenger with GNU General Public License v3.0 5 votes vote down vote up
private void generateQR() throws Exception {
    json = generateJson();

    Log.d(QRPresenterActivity.class.getSimpleName(), json);

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(json, BarcodeFormat.QR_CODE,1080,1080);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        ((ImageView) findViewById(R.id.QRView)).setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: RequestEtherActivity.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public void updateQR() {
    int qrCodeDimention = 400;
    String iban = "iban:" + selectedEtherAddress;
    if (amount.getText().toString().length() > 0 && new BigDecimal(amount.getText().toString()).compareTo(new BigDecimal("0")) > 0) {
        iban += "?amount=" + amount.getText().toString();
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    QREncoder qrCodeEncoder;
    if (prefs.getBoolean("qr_encoding_erc", true)) {
        AddressEncoder temp = new AddressEncoder(selectedEtherAddress);
        if (amount.getText().toString().length() > 0 && new BigDecimal(amount.getText().toString()).compareTo(new BigDecimal("0")) > 0)
            temp.setAmount(amount.getText().toString());
        qrCodeEncoder = new QREncoder(AddressEncoder.encodeERC(temp), null,
                Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), qrCodeDimention);
    } else {
        qrCodeEncoder = new QREncoder(iban, null,
                Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), qrCodeDimention);
    }

    try {
        Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
        qr.setImageBitmap(bitmap);
    } catch (WriterException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: Utils.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
public static Bitmap strToQR(String str, int width, int height) {
    if(str == null || str.equals("")) {
        return null;
    }
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(str, BarcodeFormat.QR_CODE,width,height);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 16
Source File: EncodingHandler.java    From QrCodeLib with MIT License 5 votes vote down vote up
/**
 * 创建二维码
 *
 * @param content   content
 * @param widthPix  widthPix
 * @param heightPix heightPix
 * @param logoBm    logoBm
 * @return 二维码
 */
public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
    try {
        if (content == null || "".equals(content)) {
            return null;
        }
        // 配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);
        int[] pixels = new int[widthPix * heightPix];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        for (int y = 0; y < heightPix; y++) {
            for (int x = 0; x < widthPix; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthPix + x] = 0xff000000;
                } else {
                    pixels[y * widthPix + x] = 0xffffffff;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
        if (logoBm != null) {
            bitmap = addLogo(bitmap, logoBm);
        }
        //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 17
Source File: SharePresenter.java    From Tok-Android with GNU General Public License v3.0 5 votes vote down vote up
private void generateQR() {
    mQrPath = StorageUtil.getShareQrCodeFile();
    String qrContent = StringUtils.formatTxFromResId(R.string.share_qr_content, mSelfAddress);
    LogUtil.i(TAG, "share qr content:" + qrContent);
    int qrCodeSize = 800;
    QRCodeEncode qrCodeEncoder =
        new QRCodeEncode(qrContent, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(),
            qrCodeSize);
    try {
        ImageUtils.compressBitmap(qrCodeEncoder.encodeAsBitmap(), mQrPath);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    mShareView.showQr(mQrPath);
}
 
Example 18
Source File: EncodingUtils.java    From tysq-android with GNU General Public License v3.0 4 votes vote down vote up
/**
     * 绘制条形码
     *
     * @param content       要生成条形码包含的内容
     * @param widthPix      条形码的宽度
     * @param heightPix     条形码的高度
     * @param isShowContent 否则显示条形码包含的内容
     * @return 返回生成条形的位图
     */
    public static Bitmap createBarcode(String content, int widthPix, int heightPix, boolean isShowContent) {
        if (TextUtils.isEmpty(content)) {
            return null;
        }
        //配置参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别 这里选择最高H级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        MultiFormatWriter writer = new MultiFormatWriter();

        try {
            // 图像数据转换,使用了矩阵转换 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
            BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.CODE_128, widthPix, heightPix, hints);

            //增加:把宽度修改我们修改过后的真实的宽度
            widthPix = bitMatrix.getWidth();
            // Log.e("zmm", "---------->" + widthPix + "--->" + height);
            int[] pixels = new int[widthPix * heightPix];
//             下面这里按照条形码的算法,逐个生成条形码的图片,
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < heightPix; y++) {
                for (int x = 0; x < widthPix; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * widthPix + x] = 0xff000000; // 黑色
                    } else {
                        pixels[y * widthPix + x] = 0xffffffff;// 白色
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
            if (isShowContent) {
                bitmap = showContent(bitmap, content);
            }
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }

        return null;
    }
 
Example 19
Source File: PrinterCommands.java    From PrinterThermal-ESCPOS-Android with MIT License 4 votes vote down vote up
/**
 * Convert a string to QR Code byte array compatible with ESC/POS printer.
 *
 * @param data String data to convert in QR Code
 * @return Bytes contain the image in ESC/POS command
 */
public static byte[] QRCodeDataToBytes(String data, int size) {

    ByteMatrix byteMatrix = null;

    try {
        EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        QRCode code = Encoder.encode(data, ErrorCorrectionLevel.L, hints);
        byteMatrix = code.getMatrix();

    } catch (WriterException e) {
        e.printStackTrace();
    }

    if (byteMatrix == null) {
        return PrinterCommands.initImageCommand(0, 0);
    }

    int
            width = byteMatrix.getWidth(),
            height = byteMatrix.getHeight(),
            coefficient = Math.round((float) size / (float) width),
            imageWidth = width * coefficient,
            imageHeight = height * coefficient,
            bytesByLine = (int) Math.ceil(((float) imageWidth) / 8f),
            i = 8;

    if (coefficient < 1) {
        return PrinterCommands.initImageCommand(0, 0);
    }

    byte[] imageBytes = PrinterCommands.initImageCommand(bytesByLine, imageHeight);

    for (int y = 0; y < height; y++) {
        byte[] lineBytes = new byte[bytesByLine];
        int j = 0, multipleX = coefficient;
        boolean isBlack = false;
        for (int x = -1; x < width;) {
            StringBuilder stringBinary = new StringBuilder();
            for (int k = 0; k < 8; k++) {
                if(multipleX == coefficient) {
                    isBlack = ++x < width && byteMatrix.get(x, y) == 1;
                    multipleX = 0;
                }
                stringBinary.append(isBlack ? "1" : "0");
                ++multipleX;
            }
            lineBytes[j++] = (byte) Integer.parseInt(stringBinary.toString(), 2);
        }

        for (int multipleY = 0; multipleY < coefficient; ++multipleY) {
            System.arraycopy(lineBytes, 0, imageBytes, i, lineBytes.length);
            i += lineBytes.length;
        }
    }

    return imageBytes;
}
 
Example 20
Source File: QrCodeUtils.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
public static Bitmap textToQrCode(String Value, int width) {
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");

    // Now with zxing version 3.2.1 you could change border size (white border size to just 1)
    hintMap.put(EncodeHintType.MARGIN, 0); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

    BitMatrix bitMatrix = null;
    try {
        bitMatrix = new MultiFormatWriter().encode(
                Value,
                BarcodeFormat.DATA_MATRIX.QR_CODE,
                width, width, hintMap
        );

    } catch (IllegalArgumentException ignored) {

    } catch (WriterException e) {
        e.printStackTrace();
    }
    int bitMatrixWidth = bitMatrix.getWidth();

    int bitMatrixHeight = bitMatrix.getHeight();

    int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

    for (int y = 0; y < bitMatrixHeight; y++) {
        int offset = y * bitMatrixWidth;

        for (int x = 0; x < bitMatrixWidth; x++) {

            pixels[offset + x] = bitMatrix.get(x, y) ?
                    Color.BLACK : Color.WHITE;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

    bitmap.setPixels(pixels, 0, width, 0, 0, bitMatrixWidth, bitMatrixHeight);

    return bitmap;
}