类com.google.zxing.MultiFormatWriter源码实例Demo

下面列出了怎么用com.google.zxing.MultiFormatWriter的API类实例代码及写法,或者点击链接到github查看源代码。

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;
}
 
源代码2 项目: Tok-Android   文件: QRCodeEncode.java
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;
}
 
源代码3 项目: QrCodeLib   文件: EncodingHandler.java
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;
}
 
源代码4 项目: QrCodeLib   文件: QrCodeGenerator.java
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;
}
 
源代码5 项目: vmqApk   文件: EncodingHandler.java
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;
}
 
源代码6 项目: MeetingFilm   文件: ZxingUtils.java
/** 将内容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;
	}
}
 
源代码7 项目: java-pay   文件: PaymentUtils.java
/**
 * 生成二维码并响应到浏览器
 *
 * @param content
 * @param response
 */
public static void createQRCode(String content, HttpServletResponse response) {
    int width = 300, height = 300;
    String format = "png";
    Map<EncodeHintType, Object> hashMap = new HashMap<>();
    hashMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8);
    hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hashMap.put(EncodeHintType.MARGIN, 1);
    try {
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("content-type", "image/png");
        response.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
        response.setDateHeader("Expires", 0);
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, width, height, hashMap);
        BufferedImage img = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ImageIO.write(img, format, response.getOutputStream());
    } catch (Exception e) {
        log.warn("create QRCode error message:{}", e.getMessage());
    }
}
 
源代码8 项目: ETHWallet   文件: MyAddressActivity.java
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;
}
 
源代码9 项目: DevUtils   文件: ZXingQRCodeUtils.java
/**
 * 同步创建指定前景色、指定背景色二维码图片
 * <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;
    }
}
 
源代码10 项目: guarda-android-wallets   文件: QrCodeUtils.java
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;
}
 
源代码11 项目: QrScan   文件: EncodingHandler.java
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;
}
 
源代码12 项目: MyBox   文件: BarcodeTools.java
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;
    }
}
 
源代码13 项目: QrCodeDemo4   文件: EncodingHandler.java
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;
}
 
源代码14 项目: springboot-pay-example   文件: PayUtil.java
/**
 * 根据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;
}
 
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;
}
 
源代码16 项目: alpha-wallet-android   文件: QRUtils.java
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;
}
 
源代码17 项目: trigger   文件: QRShowActivity.java
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();
    }
}
 
源代码18 项目: seezoon-framework-all   文件: ZxingHelper.java
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;
}
 
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;
}
 
源代码20 项目: rebuild   文件: BarCodeGenerator.java
/**
 * @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);
    }
}
 
源代码21 项目: CodeScaner   文件: EncodingHandler.java
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;
}
 
源代码22 项目: o2oa   文件: ActionCreateCode.java
/**
    *二维码实现
    * @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();
        }
}
 
源代码23 项目: o2oa   文件: ActionCreateCode.java
/**
    *二维码实现
    * @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;
}
 
源代码24 项目: bitcoinpos   文件: PaymentRequestFragment.java
Bitmap encodeAsBitmap(String str, int size) throws WriterException {
    BitMatrix result;
    try {
        result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, size, size);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    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) ? 0xFF000000 : 0xFFFFFFFF;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
 
源代码25 项目: SecScanQR   文件: HistoryDetailsActivity.java
/**
 * This method creates a picture of the scanned qr code
 */
private void showQrImage() {
    multiFormatWriter = new MultiFormatWriter();
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);

    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try{
        BarcodeFormat format = generalHandler.StringToBarcodeFormat(selectedFormat);
        BitMatrix bitMatrix = multiFormatWriter.encode(selectedCode, format, 250,250, hintMap);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
        codeImage.setImageBitmap(bitmap);
    } catch (Exception e){
        codeImage.setVisibility(View.GONE);
    }
}
 
源代码26 项目: SecScanQR   文件: ScannerActivity.java
/**
 * This method creates a picture of the scanned qr code
 */
private void showQrImage() {
    multiFormatWriter = new MultiFormatWriter();
    Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);

    hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hintMap.put(EncodeHintType.MARGIN, 1); /* default = 4 */
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    try{
        BarcodeFormat format = generalHandler.StringToBarcodeFormat(qrcodeFormat);
        BitMatrix bitMatrix = multiFormatWriter.encode(qrcode, format, 250,250, hintMap);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
        codeImage.setImageBitmap(bitmap);
    } catch (Exception e){
        codeImage.setVisibility(View.GONE);
    }
}
 
源代码27 项目: Lunary-Ethereum-Wallet   文件: QREncoder.java
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;
}
 
源代码28 项目: Mobike   文件: EncodingHandler.java
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;
}
 
源代码29 项目: wish-pay   文件: ZxingUtils.java
/**
 * 将内容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;
    }
}
 
源代码30 项目: wish-pay   文件: ZxingUtils.java
/**
 * 将内容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();
        }
    }
}
 
 类所在包
 类方法
 同包方法