public static Bitmap getQR(final byte[] content, final int size) { final QRCodeWriter writer = new QRCodeWriter(); try { final Map<EncodeHintType, Object> encodingHints = new HashMap<>(); encodingHints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); encodingHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); final BitMatrix encoding = writer.encode(Utils.makeQR(content), BarcodeFormat.QR_CODE, size, size, encodingHints); final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { bitmap.setPixel(i, j, encoding.get(i, j) ? COLOR_DARK : COLOR_LIGHT); } } return bitmap; } catch (WriterException e) { Log.e("QRUtils", "Failed to get QR code", e); } return null; }
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; }
/** * Zxing图形码生成工具 * * @param contents * 内容 * @param barcodeFormat * BarcodeFormat对象 * @param format * 图片格式,可选[png,jpg,bmp] * @param width * 宽 * @param height * 高 * @param margin * 边框间距px * @param saveImgFilePath * 存储图片的完整位置,包含文件名 * @return {boolean} */ public static boolean encode(String contents, BarcodeFormat barcodeFormat, Integer margin, ErrorCorrectionLevel errorLevel, String format, int width, int height, String saveImgFilePath) { Boolean bool = false; BufferedImage bufImg; Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, errorLevel); hints.put(EncodeHintType.MARGIN, margin); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); try { // contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1"); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height, hints); MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF); bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config); bool = writeToFile(bufImg, format, saveImgFilePath); } catch (Exception e) { e.printStackTrace(); } return bool; }
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; }
/** * 创建二维码 * * @param size 二维码尺寸 * @param content 二维码内容 * @param logoPath Logo地址 * @param needCompress 是否压缩 * @return * @throws Exception */ private static BufferedImage createImage(int size, String content, String logoPath, 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, size, 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 (logoPath == null || "".equals(logoPath)) { return image; } // 插入图片 QRCodeUtils.insertImage(size, image, logoPath, needCompress); return image; }
public Bitmap encodeAsBitmap() throws WriterException { if (!encoded) return null; Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(contents); hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); if (encoding != null) { hints.put(EncodeHintType.CHARACTER_SET, encoding); } hints.put(EncodeHintType.MARGIN, 2); /* default = 4 */ 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; }
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.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; }
public static Bitmap createQRCode(String content, int widthAndHeight) { if (TextUtils.isEmpty(content)) return null; try { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Fault tolerance level. MAX hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //Set the width of the blank margin. hints.put(EncodeHintType.MARGIN, 0); BitMatrix matrix = new MultiFormatWriter().encode(content, 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] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
/** * 根据内容生成二维码数据 * @param content 二维码文字内容[为了信息安全性,一般都要先进行数据加密] * @param width 二维码照片宽度 * @param height 二维码照片高度 * @return */ public static BitMatrix createQRCode(String content, int width, int height){ Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); //设置字符编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.MARGIN, 1); BitMatrix matrix = null; try { matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { e.printStackTrace(); } return matrix; }
/** * 用字符串生成二维码 * * @param str * @author J!nl!n * @return * @throws WriterException */ public Bitmap Create2DCode(String str) 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, 480, 480, hints); 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] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
private Bitmap createQRCode(String str, int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 使用utf8编码 BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);// 这里需要把hints传进去,否则会出现中文乱码 int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; // 上色,如果不做保存二维码、分享二维码等功能,上白色部分可以不写。至于原因,在生成图片的时候,如果没有指定颜色,其会使用系统默认颜色来上色,很多情况就会出现保存的二维码图片全黑 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) {// 有数据的像素点使用黑色 pixels[y * width + x] = BLACK; } else {// 其他部分则使用白色 pixels[y * width + x] = WHITE; } } } //生成bitmap Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
/**二维码的生成*/ private void createQRCode(HttpServletResponse response){ /**设置二维码的参数*/ Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 留白区域大小 hints.put(EncodeHintType.MARGIN, margin); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints); // 输出二维码 MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream()); } catch (Exception e) { System.out.println("生成二维码异常!"+e.toString()); } }
public static String createQrcode(String dir, String _text) { String qrcodeFilePath = ""; try { int qrcodeWidth = 300; int qrcodeHeight = 300; String qrcodeFormat = "png"; HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(_text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints); BufferedImage image = new BufferedImage(qrcodeWidth, qrcodeHeight, BufferedImage.TYPE_INT_RGB); File qrcodeFile = new File(dir + "/" + UUID.randomUUID().toString() + "." + qrcodeFormat); ImageIO.write(image, qrcodeFormat, qrcodeFile); MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, qrcodeFile.toPath()); qrcodeFilePath = qrcodeFile.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); } return qrcodeFilePath; }
private Bitmap endcode(String input) { BarcodeFormat format = BarcodeFormat.QR_CODE; EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hint.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix result = null; try { result = new MultiFormatWriter().encode(input, BarcodeFormat.QR_CODE, 500, 500, hint); } catch (WriterException e) { e.printStackTrace(); } int w = result.getWidth(); int h = result.getHeight(); int[] pixels = new int[w * h]; for (int y = 0; y < h; y++) { int offset = y * w; for (int x = 0; x < w; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bit = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); bit.setPixels(pixels, 0, w, 0, 0, w, h); ImageView imageView = (ImageView) findViewById(R.id.qr_code_id); imageView.setImageBitmap(bit); return bit; }
public static String getQr(String text) { String s = "生成二维码失败"; int width = 40; int height = 40; // 用于设置QR二维码参数 Hashtable<EncodeHintType, Object> qrParam = new Hashtable<EncodeHintType, Object>(); // 设置QR二维码的纠错级别——这里选择最低L级别 qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); qrParam.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, qrParam); s = toAscii(bitMatrix); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
/** * Method to get the JavaFX Image QR code for easy input of address * @param width width of the image * @param height height of the image * @return Java FX Image * @throws IOException Either when there is an encoding error or java's reserved memory is overwritten. * @throws WriterException When ZXING encounters an error. */ public Image getQR(int width, int height) throws IOException, WriterException{ String charset = "UTF-8"; Map hintMap = new HashMap(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); ByteArrayOutputStream stream = new ByteArrayOutputStream(); BitMatrix matrix = new MultiFormatWriter().encode(new String(cryptoAddress.getBytes(charset), charset), BarcodeFormat.QR_CODE, width, height, hintMap); MatrixToImageWriter.writeToStream(matrix, "png", stream); stream.flush(); byte[] data = stream.toByteArray(); stream.close(); return new Image(new ByteArrayInputStream(data)); }
/** * 二维码信息写成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; }
/** * 将内容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; } }
public QRCodeMaker (List<String> beds) throws WriterException, IOException { String url; String bedPath; Map hintMap = new HashMap(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); System.out.println(System.getProperty("user.dir")); System.out.println("About to create the directory"); File imageDirectory = new File(imageDirectoryPath); imageDirectory.delete(); imageDirectory.mkdir(); System.out.println("Made the directory: " + imageDirectoryPath); for (String bed : beds){ bedPath = imageDirectoryPath + bed + ".jpg"; url = "http://174.138.66.146:9000/api/" + bed; createQRCode(url, bedPath, charset, hintMap, 500, 500); } }
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception { Hashtable 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; } // 插入图片 CodeUtil.insertImage(image, imgPath, needCompress); return image; }
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); }
/** * 二维码编码 * <p> * String imgPath2 = "target/zxing2.png"; * String contents2 = "Hello Zxing!"; * int width2 = 300, height2 = 300; * ZxingHelper.encode2(contents2, width2, height2, imgPath2); * * @param contents 内容 * @param width 宽度 * @param height 高度 * @param imgPath 生成图片路径 */ public static void encode2(String contents, int width, int height, String imgPath) { EnumMap<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "GBK"); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter .writeToPath(bitMatrix, "png", new File(imgPath).toPath()); } catch (Exception e) { LOGGER.error("生成二维码错误", e); } }
/** * 关于Ansi Color的信息,参考:http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html * * @param text */ public static void printQRCodeInTerminal(String text) { int qrcodeWidth = 400; int qrcodeHeight = 400; HashMap<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); try { BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints); // 将像素点转成格子 int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); for (int y = 30; y < height - 30; y += 10) { for (int x = 30; x < width - 20; x += 10) { boolean isBlack = bitMatrix.get(x, y); System.out.print(isBlack ? " " : "\u001b[47m \u001b[0m"); } System.out.println(); } } catch (WriterException e) { e.printStackTrace(); } }
public static Bitmap createQRCode(String str, int size) 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, size, size); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for(int x = 0; x < width; x ++){ for(int y = 0; y < height; y ++){ if(matrix.get(x, y)){ pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
/** * 生成二维码 * * @param content 二维码内容 * @param size 二维码内容大小 * @param color 二维码颜色 */ public static Bitmap createQRCode(String content, int size,int bgColor, int color) { try { Hashtable<EncodeHintType, String> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, CHARACTER_SET); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints); int[] pixels = new int[size * size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (bitMatrix.get(x, y)) { pixels[y * size + x] = color; } else { pixels[y * size + x] = bgColor; } } } Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, size, 0, 0, size, size); return bitmap; } catch (WriterException e) { return null; } }
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; }
/** * 二维码生成 ,储存地址qrcodeFilePath * * @param text 二维码内容 * @return Base64Code String */ @SuppressWarnings("restriction") public String createQrcode(String text){ String Imgencode=""; byte[] imageByte=null; String formatName="PNG"; try { // int qrcodeWidth = 300; int qrcodeHeight = 300; HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints); ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); ImageIO.write(bufferedImage, formatName, out); imageByte=out.toByteArray(); Imgencode = new sun.misc.BASE64Encoder().encode(imageByte); } catch (Exception e) { e.printStackTrace(); } return Imgencode; }
/** * * @param path 完整的路径包括文件名 * @param width 图片宽 * @param height 图片高 * @param contents 内容 */ public static void createImage(String path,int width,int height,String contents){ String format = "png"; //定义二维码的参数 HashMap hints = new HashMap(); hints.put(EncodeHintType.AZTEC_LAYERS, "utf-8");//设置编码 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//设置容错等级: L M Q H hints.put(EncodeHintType.MARGIN,1);//设置边距 //生成文件 try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,hints); Path file = new File(path).toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); } catch (Exception e) { e.printStackTrace(); } }
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; }
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"); } else if (width < 0 || height < 0) { throw new IllegalArgumentException("Negative size is not allowed. Input: " + width + 'x' + height); } else { int sidesMargin = getDefaultMargin(); if (hints != null) { Integer sidesMarginInt = (Integer) hints.get(EncodeHintType.MARGIN); if (sidesMarginInt != null) { sidesMargin = sidesMarginInt.intValue(); } } return renderResult(encode(contents), width, height, sidesMargin); } }
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) { if (contents.isEmpty()) { throw new IllegalArgumentException("Found empty contents"); } else if (format != BarcodeFormat.DATA_MATRIX) { throw new IllegalArgumentException("Can only encode DATA_MATRIX, but got " + format); } else if (width < 0 || height < 0) { throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height); } else { SymbolShapeHint shape = SymbolShapeHint.FORCE_NONE; Dimension minSize = null; Dimension maxSize = null; if (hints != null) { SymbolShapeHint requestedShape = (SymbolShapeHint) hints.get(EncodeHintType .DATA_MATRIX_SHAPE); if (requestedShape != null) { shape = requestedShape; } Dimension requestedMinSize = (Dimension) hints.get(EncodeHintType.MIN_SIZE); if (requestedMinSize != null) { minSize = requestedMinSize; } Dimension requestedMaxSize = (Dimension) hints.get(EncodeHintType.MAX_SIZE); if (requestedMaxSize != null) { maxSize = requestedMaxSize; } } String encoded = HighLevelEncoder.encodeHighLevel(contents, shape, minSize, maxSize); SymbolInfo symbolInfo = SymbolInfo.lookup(encoded.length(), shape, minSize, maxSize, true); DefaultPlacement placement = new DefaultPlacement(ErrorCorrection.encodeECC200 (encoded, symbolInfo), symbolInfo.getSymbolDataWidth(), symbolInfo .getSymbolDataHeight()); placement.place(); return encodeLowLevel(placement, symbolInfo); } }
private static void generateQRCode(final OutputStream stream, final String key) { try { final Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class); hintMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name()); hintMap.put(EncodeHintType.MARGIN, 2); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); final QRCodeWriter qrCodeWriter = new QRCodeWriter(); final BitMatrix byteMatrix = qrCodeWriter.encode(key, BarcodeFormat.QR_CODE, 250, 250, hintMap); final int width = byteMatrix.getWidth(); final BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB); image.createGraphics(); final Graphics2D graphics = (Graphics2D) image.getGraphics(); try { graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, width, width); graphics.setColor(Color.BLACK); IntStream.range(0, width) .forEach(i -> IntStream.range(0, width) .filter(j -> byteMatrix.get(i, j)) .forEach(j -> graphics.fillRect(i, j, 1, 1))); } finally { graphics.dispose(); } ImageIO.write(image, "png", stream); } catch (final Exception e) { throw Throwables.propagate(e); } }
public static byte[] generate(String address, int size) { try { Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hintMap.put(EncodeHintType.MARGIN, 0); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); QRCodeWriter qrWriter = new QRCodeWriter(); BitMatrix qrMatrix = qrWriter.encode(address, BarcodeFormat.QR_CODE, size, size, hintMap); int width = qrMatrix.getWidth(); BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, width, width); graphics.setColor(Color.BLACK); for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { if (qrMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, IMAGE_FILE_TYPE, bos); return bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; }
Bitmap encodeAsBitmap() throws WriterException { String contentsToEncode = contents; if (contentsToEncode == null) { return null; } Map<EncodeHintType,Object> hints = null; String encoding = guessAppropriateEncoding(contentsToEncode); if (encoding != null) { hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } BitMatrix result; try { result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints); } 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) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.EAN_8) { throw new IllegalArgumentException("Can only encode EAN_8, but got " + format); } return super.encode(contents, format, width, height, hints); }