com.google.zxing.MultiFormatWriter Java Examples

The following examples show how to use com.google.zxing.MultiFormatWriter. 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: QRCodeGenerator.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 9 votes vote down vote up
public static Bitmap transform(byte[] inputData) {
    String encoded;
    encoded = Base64.encodeToString(inputData, Base64.DEFAULT);

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    Bitmap bitmap = null;
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(encoded, BarcodeFormat.QR_CODE, 1000, 1000);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
    } catch (WriterException e) {
        Timber.e(e);
    }

    return bitmap;
}
 
Example #2
Source File: EncodingHandler.java    From vmqApk with MIT License 7 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
	Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
	BitMatrix matrix = new MultiFormatWriter().encode(str,
			BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
	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;
			}
		}
	}
	Bitmap bitmap = Bitmap.createBitmap(width, height,
			Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example #3
Source File: PayUtil.java    From springboot-pay-example with Apache License 2.0 6 votes vote down vote up
/**
 * 根据url生成二位图片对象
 *
 * @param codeUrl
 * @return
 * @throws WriterException
 */
public static BufferedImage getQRCodeImge(String codeUrl) throws WriterException {
    Map<EncodeHintType, Object> hints = new Hashtable();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
    int width = 256;
    BitMatrix bitMatrix = (new MultiFormatWriter()).encode(codeUrl, BarcodeFormat.QR_CODE, width, width, hints);
    BufferedImage image = new BufferedImage(width, width, 1);
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < width; ++y) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? -16777216 : -1);
        }
    }

    return image;
}
 
Example #4
Source File: EncodingHandler.java    From QrCodeLib with MIT License 6 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    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;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example #5
Source File: ZXingQRCodeUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 同步创建指定前景色、指定背景色二维码图片
 * <pre>
 *     该方法是耗时操作, 请在子线程中调用
 * </pre>
 * @param content         生成内容
 * @param size            图片宽高大小 ( 正方形 px )
 * @param foregroundColor 二维码图片的前景色
 * @param backgroundColor 二维码图片的背景色
 * @return 二维码图片
 */
public static Bitmap syncEncodeQRCode(final String content, final int size, final int foregroundColor, final int backgroundColor) {
    try {
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, ENCODE_HINTS);
        int[] pixels = new int[size * size];
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * size + x] = foregroundColor;
                } else {
                    pixels[y * size + x] = backgroundColor;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
        return bitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "syncEncodeQRCode");
        return null;
    }
}
 
Example #6
Source File: QRCodeEncode.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example #7
Source File: KeyUriFormatTest.java    From MaxKey with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) { 
    try {  
    	KeyUriFormat kuf=new KeyUriFormat(KeyUriFormat.Types.TOTP,
    			"GIWVWOL7EI5WLVZPDMROEPSTFBEVO77Q",
    			"connsec.com");
    	kuf.setPeriod(60);
        String path = "D:\\totp.png";  
        BitMatrix byteMatrix;  
        byteMatrix = new MultiFormatWriter().encode(new String(kuf.format("shiming").getBytes("GBK"),"iso-8859-1"),  
                BarcodeFormat.QR_CODE, 300, 300);  
        File file = new File(path);  
          
        QRCode.writeToPath(byteMatrix, "png", file);  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}
 
Example #8
Source File: ZxingUtils.java    From MeetingFilm with Apache License 2.0 6 votes vote down vote up
/** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
    */
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
	try {
           Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
           hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
           hints.put(EncodeHintType.CHARACTER_SET, "UTF8");

		BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

           File imageFile = new File(imgPath);
		writeToFile(bitMatrix, "png", imageFile);

           return imageFile;

	} catch (Exception e) {
		log.error("create QR code error!", e);
           return null;
	}
}
 
Example #9
Source File: QRCodeUtil.java    From JavaWeb with Apache License 2.0 6 votes vote down vote up
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
    Hashtable<EncodeHintType,Object> hints = new Hashtable<>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
                    : 0xFFFFFFFF);
        }
    }
    if (imgPath == null || "".equals(imgPath)) {
        return image;
    }
    //插入图片
    QRCodeUtil.insertImage(image, imgPath, needCompress);
    return image;
}
 
Example #10
Source File: EncodingHandler.java    From vinci with Apache License 2.0 6 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    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;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example #11
Source File: EncodingHandler.java    From smart-farmer-android with Apache License 2.0 6 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    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;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example #12
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 #13
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 #14
Source File: EncodingHandler.java    From QrScan with Apache License 2.0 6 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
	Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	BitMatrix matrix = new MultiFormatWriter().encode(str,
			BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
	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;
			}
		}
	}
	Bitmap bitmap = Bitmap.createBitmap(width, height,
			Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example #15
Source File: BarcodeTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static BufferedImage DataMatrix(String code,
        int dmWidth, int dmHeigh) {
    try {
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
                BarcodeFormat.DATA_MATRIX, dmWidth, dmHeigh, hints);

        return MatrixToImageWriter.toBufferedImage(bitMatrix);

    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example #16
Source File: EncodingHandler.java    From QrCodeDemo4 with MIT License 6 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
	Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
	BitMatrix matrix = new MultiFormatWriter().encode(str,
			BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
	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;
			}
		}
	}
	Bitmap bitmap = Bitmap.createBitmap(width, height,
			Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example #17
Source File: EncodingHandler.java    From CodeScaner with MIT License 6 votes vote down vote up
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
	Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	BitMatrix matrix = new MultiFormatWriter().encode(str,
			BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
	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;
			}
		}
	}
	Bitmap bitmap = Bitmap.createBitmap(width, height,
			Bitmap.Config.ARGB_8888);
	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
	return bitmap;
}
 
Example #18
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 二维码信息写成JPG BufferedImage
 *
 * @param content
 * @param width
 * @param height
 * @return
 */
public static BufferedImage writeInfoToJpgBuffImg(String content, int width, int height) {
    if (width < 250) {
        width = 250;
    }
    if (height < 250) {
        height = 250;
    }
    BufferedImage re = null;

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    Map hints = new HashMap();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);
        re = MatrixToImageWriter.toBufferedImage(bitMatrix);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return re;
}
 
Example #19
Source File: RedeemSignatureDisplayActivity.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private Bitmap createQRImage(String address) {
    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize(size);
    int imageSize = (int) (size.x * QR_IMAGE_WIDTH_RATIO);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                address,
                BarcodeFormat.QR_CODE,
                imageSize,
                imageSize,
                null);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (Exception e) {
        Toast.makeText(this, getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
                .show();
    }
    return null;
}
 
Example #20
Source File: QRUtils.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
public static Bitmap createQRImage(Context context, String address, int imageSize) {
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                address,
                BarcodeFormat.QR_CODE,
                imageSize,
                imageSize,
                null);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (Exception e) {
        Toast.makeText(context, context.getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
                .show();
    }
    return null;
}
 
Example #21
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 将内容contents生成长为width,宽为width的图片,返回刘文静
 */
public static ServletOutputStream getQRCodeImge(String contents, int width, int height) throws IOException {
    ServletOutputStream stream = null;
    try {
        Map<EncodeHintType, Object> hints = Maps.newHashMap();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", stream);
        return stream;
    } catch (Exception e) {
        log.error("create QR code error!", e);
        return null;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
 
Example #22
Source File: QRShowActivity.java    From trigger with GNU General Public License v2.0 6 votes vote down vote up
private void generateQR(Setup setup) throws Exception {
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    int data_length = 0;
    try {
        JSONObject obj = Settings.toJsonObject(setup);
        String data = encodeSetup(obj);
        data_length = data.length();

        // data has to be a string
        BitMatrix bitMatrix = multiFormatWriter.encode(data, BarcodeFormat.QR_CODE, 1080, 1080);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
        ((ImageView) findViewById(R.id.QRView)).setImageBitmap(bitmap);
    } catch (WriterException e) {
        Toast.makeText(this, e.getMessage() + " (" + data_length + " Bytes)", Toast.LENGTH_LONG).show();
        finish();
    }
}
 
Example #23
Source File: ZxingHelper.java    From seezoon-framework-all with Apache License 2.0 6 votes vote down vote up
private BufferedImage createImage(String content, InputStream logo, boolean needCompress) throws Exception {
	Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
	hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
	hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
	hints.put(EncodeHintType.MARGIN, 1);
	BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, imageSize, imageSize,
			hints);
	int width = bitMatrix.getWidth();
	int height = bitMatrix.getHeight();
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	for (int x = 0; x < width; x++) {
		for (int y = 0; y < height; y++) {
			image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
		}
	}
	if (logo == null) {
		return image;
	}
	// 插入logo
	this.insertLogo(image, logo, needCompress);
	return image;
}
 
Example #24
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
 */
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
    try {
        Map<EncodeHintType, Object> hints = Maps.newHashMap();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");

        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

        File imageFile = new File(imgPath);
        writeToFile(bitMatrix, "png", imageFile);

        return imageFile;

    } catch (Exception e) {
        log.error("create QR code error!", e);
        return null;
    }
}
 
Example #25
Source File: MyAddressActivity.java    From trust-wallet-android-source with GNU General Public License v3.0 6 votes vote down vote up
private Bitmap createQRImage(String address) {
    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize(size);
    int imageSize = (int) (size.x * QR_IMAGE_WIDTH_RATIO);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                address,
                BarcodeFormat.QR_CODE,
                imageSize,
                imageSize,
                null);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        return barcodeEncoder.createBitmap(bitMatrix);
    } catch (Exception e) {
        Toast.makeText(this, getString(R.string.error_fail_generate_qr), Toast.LENGTH_SHORT)
            .show();
    }
    return null;
}
 
Example #26
Source File: BarCodeGenerator.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param content
 * @param format
 * @param height
 * @return
 */
public static BitMatrix createBarCode(String content, BarcodeFormat format, int height) {
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.MARGIN, 0);

    // 条形码宽度为自适应
    int width = format == BarcodeFormat.QR_CODE ? height : 0;
    try {
        return new MultiFormatWriter().encode(content, format, width, height, hints);

    } catch (WriterException ex) {
        throw new RebuildException("Encode BarCode failed : " + content, ex);
    }
}
 
Example #27
Source File: ActionCreateCode.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
    *二维码实现
    * @param msg /二维码包含的信息
    */
public static byte[] getBarCodeWo(String msg){
		byte[] bs = null;
        try {
            String format = "png";
            Map<EncodeHintType,String> map =new HashMap<EncodeHintType, String>();
            //设置编码 EncodeHintType类中可以设置MAX_SIZE, ERROR_CORRECTION,CHARACTER_SET,DATA_MATRIX_SHAPE,AZTEC_LAYERS等参数
            map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
            map.put(EncodeHintType.MARGIN,"1");
            //生成二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE,300,300,map);
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(image, "gif", out);
			bs = out.toByteArray();
            
        }catch (Exception e) {
            e.printStackTrace();
        }
        return bs;
}
 
Example #28
Source File: EncodingHandler.java    From Mobike with Apache License 2.0 6 votes vote down vote up
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
    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;
            }
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
Example #29
Source File: ActionCreateCode.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
    *二维码实现
    * @param msg /二维码包含的信息
    * @param path /二维码存放路径
    */
public static void getBarCode(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            String format = "png";
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  
            Map<EncodeHintType,String> map =new HashMap<EncodeHintType, String>();
            //设置编码 EncodeHintType类中可以设置MAX_SIZE, ERROR_CORRECTION,CHARACTER_SET,DATA_MATRIX_SHAPE,AZTEC_LAYERS等参数
            map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
            map.put(EncodeHintType.MARGIN,"1");
            //生成二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE,300,300,map);
            MatrixToImageWriter.writeToStream(bitMatrix,format,ous);
        }catch (Exception e) {
            e.printStackTrace();
        }
}
 
Example #30
Source File: QREncoder.java    From Lunary-Ethereum-Wallet with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}