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

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

源代码1 项目: 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;
}
 
源代码2 项目: 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;
}
 
源代码3 项目: 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;
}
 
源代码4 项目: 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;
}
 
源代码5 项目: 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;
	}
}
 
源代码6 项目: AndroidWallet   文件: QRCodeHelper.java

public static Bitmap generateBitmap(String content, int width, int height) {
    int margin = 5;  //自定义白边边框宽度
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    Hashtable hints = new Hashtable<>();
    hints.put(EncodeHintType.MARGIN, 0);
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    try {
        BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
      //  encode = updateBit(encode, margin);  //生成新的bitMatrix
        int[] pixels = new int[width * height];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (encode.get(j, i)) {
                    pixels[i * width + j] = 0x00000000;
                } else {
                    pixels[i * width + j] = 0xffffffff;
                }
            }
        }
        return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
源代码7 项目: 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();
        }
    }
}
 
源代码8 项目: Auditor   文件: AttestationActivity.java

private Bitmap createQrCode(final byte[] contents) {
    final BitMatrix result;
    try {
        final QRCodeWriter writer = new QRCodeWriter();
        final Map<EncodeHintType,Object> hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.ISO_8859_1);
        final int size = Math.min(imageView.getWidth(), imageView.getHeight());
        result = writer.encode(new String(contents, StandardCharsets.ISO_8859_1), BarcodeFormat.QR_CODE,
                size, size, hints);
    } catch (WriterException e) {
        throw new RuntimeException(e);
    }

    final int width = result.getWidth();
    final int height = result.getHeight();
    final int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        final int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565);
}
 
源代码9 项目: 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());
    }
}
 
源代码10 项目: 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;
}
 
源代码11 项目: 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;
    }
}
 
源代码12 项目: 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;
}
 
源代码13 项目: 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;
}
 

/**
 * Encode the contents following specified format.
 * {@code width} and {@code height} are required size. This method may return bigger size
 * {@code BitMatrix} when specified size is too small. The user can set both {@code width} and
 * {@code height} to zero to get minimum size barcode. If negative value is set to {@code width}
 * or {@code height}, {@code IllegalArgumentException} is thrown.
 */
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {
  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Negative size is not allowed. Input: "
                                           + width + 'x' + height);
  }

  int sidesMargin = getDefaultMargin();
  if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) {
    sidesMargin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
  }

  boolean[] code = encode(contents);
  return renderResult(code, width, height, sidesMargin);
}
 
源代码15 项目: 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;
}
 
源代码16 项目: 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);
    }
}
 
源代码17 项目: 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;
}
 
源代码18 项目: code-scanner   文件: BarcodeUtils.java

/**
 * Encode text content
 *
 * @param content Text to be encoded
 * @param format  Result barcode format
 * @param width   Result image width
 * @param height  Result image height
 * @param hints   Encoder hints
 * @return Barcode bit matrix, if it was encoded successfully, {@code null} otherwise
 * @see EncodeHintType
 * @see BitMatrix
 */
@Nullable
public static BitMatrix encodeBitMatrix(@NonNull final String content,
        @NonNull final BarcodeFormat format, final int width, final int height,
        @Nullable final Map<EncodeHintType, ?> hints) {
    Objects.requireNonNull(content);
    Objects.requireNonNull(format);
    final MultiFormatWriter writer = new MultiFormatWriter();
    try {
        if (hints != null) {
            return writer.encode(content, format, width, height, hints);
        } else {
            return writer.encode(content, format, width, height);
        }
    } catch (final WriterException e) {
        return null;
    }
}
 
源代码19 项目: 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;
}
 
源代码20 项目: java-tutorial   文件: QRCodeUtilTest.java

private BarcodeParamDTO initBarcodeParam() {
	BarcodeParamDTO paramDTO = new BarcodeParamDTO();
	paramDTO.setWidth(200);
	paramDTO.setHeight(200);
	paramDTO.setFilepath(qrcodeFile);
	paramDTO.setImageFormat("png");
	paramDTO.setBarcodeFormat(BarcodeFormat.QR_CODE);

	// 编码参数
	Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
	encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
	paramDTO.setEncodeHints(encodeHints);

	// 解码参数
	HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
	decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
	paramDTO.setDecodeHints(decodeHints);

	return paramDTO;
}
 
源代码21 项目: letv   文件: PDF417Writer.java

public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (format != BarcodeFormat.PDF_417) {
        throw new IllegalArgumentException("Can only encode PDF_417, but got " + format);
    }
    PDF417 encoder = new PDF417();
    if (hints != null) {
        if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) {
            encoder.setCompact(((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)).booleanValue());
        }
        if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) {
            encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION));
        }
        if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) {
            Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS);
            encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows());
        }
    }
    return bitMatrixFromEncoder(encoder, contents, width, height);
}
 
源代码22 项目: letv   文件: QRCodeWriter.java

public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (contents.length() == 0) {
        throw new IllegalArgumentException("Found empty contents");
    } else if (format != BarcodeFormat.QR_CODE) {
        throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
    } else if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    } else {
        ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
        int quietZone = 4;
        if (hints != null) {
            ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
            if (requestedECLevel != null) {
                errorCorrectionLevel = requestedECLevel;
            }
            Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
            if (quietZoneInt != null) {
                quietZone = quietZoneInt.intValue();
            }
        }
        return renderResult(Encoder.encode(contents, errorCorrectionLevel, hints), width, height, quietZone);
    }
}
 
源代码23 项目: wish-pay   文件: ZxingUtils.java

/**
 * 二维码信息写成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;
}
 
源代码24 项目: xmrwallet   文件: ReceiveFragment.java

public Bitmap generate(String text, int width, int height) {
    if ((width <= 0) || (height <= 0)) return null;
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    try {
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        int[] pixels = new int[width * height];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (bitMatrix.get(j, i)) {
                    pixels[i * width + j] = 0x00000000;
                } else {
                    pixels[i * height + j] = 0xffffffff;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        bitmap = addLogo(bitmap);
        return bitmap;
    } catch (WriterException ex) {
        Timber.e(ex);
    }
    return null;
}
 
源代码25 项目: 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;
    }
}
 
源代码26 项目: 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;
}
 
源代码27 项目: PHONK   文件: PMedia.java

public Bitmap generateQRCode(String text) {
    Bitmap bmp = null;

    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage

    int size = 256;

    BitMatrix bitMatrix = null;
    try {
        bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);

        int width = bitMatrix.getWidth();
        bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < width; y++) {
                bmp.setPixel(y, x, bitMatrix.get(x, y) == true ? Color.BLACK : Color.WHITE);
            }
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return bmp;
}
 
源代码28 项目: jeewx-boot   文件: CommonController.java

/**
 * @功能:下载二维码
 * @作者:liwenhui 
 * @时间:2018-3-19 下午04:18:59
 * @修改:
 * @param url 生成二维码的URL
 * @param fileName 生成文件的名字
 * @param response
 * @throws Exception  
 */
@SkipAuth(auth= SkipPerm.SKIP_SIGN)
@RequestMapping(value = "downloadQRCode", method ={RequestMethod.GET,RequestMethod.POST})
public void downloadQRCode(@RequestParam(required = true, value = "url") String url,@RequestParam(required=true,value="fileName")String fileName, HttpServletResponse response, HttpServletRequest request) throws Exception {
	String text = url;
	int width = 500; // 二维码图片宽度
       int height = 500; // 二维码图片高度
       String format = "jpg";// 二维码的图片格式
       Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
       BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width, height, hints);
       //文件名
       fileName+=".jpg";
	// 制定浏览器头
	// 在下载的时候这里是英文是没有问题的
	// 如果图片名称是中文需要设置转码
	response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
	OutputStream out = response.getOutputStream();;
	try {
		// 读取文件
		MatrixToImageWriter.writeToStream(bitMatrix, format, out);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (out != null)
			out.close();
	}
}
 
源代码29 项目: jeewx-boot   文件: JwSystemActController.java

/**
 *  活动二维码下载 (通用方法)
 * @param url  :二维码网址
 */
@RequestMapping(value = "downMatrix", method ={RequestMethod.GET,RequestMethod.POST})
public void downMatrix(@RequestParam(required = true, value = "qrCodeName") String qrCodeName,@RequestParam(required = true, value = "url") String url, HttpServletResponse response, HttpServletRequest request) throws Exception {
	String text = url;
	int width = 500; // 二维码图片宽度
    int height = 500; // 二维码图片高度
    String format = "jpg";// 二维码的图片格式
    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
    BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width, height, hints);
        
        
	// 获取文件名
    String fileName = "";
    if(qrCodeName!=null)
    {
    	fileName = qrCodeName+".jpg";
    }else{
    	fileName = "活动二维码.jpg";
    }
	// 制定浏览器头
	// 在下载的时候这里是英文是没有问题的
	// 如果图片名称是中文需要设置转码
	response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
	OutputStream out = response.getOutputStream();;
	try {
		// 读取文件
		MatrixToImageWriter.writeToStream(bitMatrix, format, out);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (out != null)
			out.close();
	}
}
 
源代码30 项目: QrCodeLib   文件: EncodingHandler.java

/**
 * 创建二维码
 *
 * @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;
}
 
 类所在包
 类方法
 同包方法