读书人

java开发_生成/解析自个儿的QRCode二维

发布时间: 2013-04-02 12:35:26 作者: rapoo

java开发_生成/解析自己的QRCode二维码
?CODE_128:?? ? ? ?java开发_生成/解析自个儿的QRCode二维码

PDF_417:

java开发_生成/解析自个儿的QRCode二维码

二维码的意思是:

java开发_生成/解析自个儿的QRCode二维码

下面是操作步骤:

一:下载zxing的压缩包:

可以到这里下载:http://code.google.com/p/zxing/downloads/list

ZXing-2.1.zip:http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.1.zip&can=2&q=

得到:

zxing-2.1\core\core.jar

zxing-2.1\javase\javase.jar

二:把他添加进入你的项目的里面:

java开发_生成/解析自个儿的QRCode二维码

/QRCodes/src/com/b510/qrcode/QRCode.java

?

92 } 93 return writer.encode(contents, format, width, height, hints); 94 } 95 96 */ 97 test.encode("helloworld,I'm Hongten.welcome to my zone:http://www.cnblogs.com/hongten", file, BarcodeFormat.QR_CODE, 200, 200, null); 98 test.decode(file); 99 }100 101 /**102 * 生成QRCode二维码<br> 103 * 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的<br>104 * static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>105 * 修改为UTF-8,否则中文编译后解析不了<br>106 */107 public void encode(String contents, File file, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {108 try {109 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);110 writeToFile(bitMatrix, "png", file);111 } catch (Exception e) {112 e.printStackTrace();113 }114 }115 116 /**117 * 生成二维码图片<br>118 * 119 * @param matrix120 * @param format121 * 图片格式122 * @param file123 * 生成二维码图片位置124 * @throws IOException125 */126 public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {127 BufferedImage image = toBufferedImage(matrix);128 ImageIO.write(image, format, file);129 }130 131 /**132 * 生成二维码内容<br>133 * 134 * @param matrix135 * @return136 */137 public static BufferedImage toBufferedImage(BitMatrix matrix) {138 int width = matrix.getWidth();139 int height = matrix.getHeight();140 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);141 for (int x = 0; x < width; x++) {142 for (int y = 0; y < height; y++) {143 image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);144 }145 }146 return image;147 }148 149 /**150 * 解析QRCode二维码151 */152 @SuppressWarnings("unchecked")153 public void decode(File file) {154 try {155 BufferedImage image;156 try {157 image = ImageIO.read(file);158 if (image == null) {159 System.out.println("Could not decode image");160 }161 LuminanceSource source = new BufferedImageLuminanceSource(image);162 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));163 Result result;164 @SuppressWarnings("rawtypes")165 Hashtable hints = new Hashtable();166 //解码设置编码方式为:utf-8167 hints.put(DecodeHintType.CHARACTER_SET, "utf-8");168 result = new MultiFormatReader().decode(bitmap, hints);169 String resultStr = result.getText();170 System.out.println("解析后内容:" + resultStr);171 } catch (IOException ioe) {172 System.out.println(ioe.toString());173 } catch (ReaderException re) {174 System.out.println(re.toString());175 }176 } catch (Exception ex) {177 System.out.println(ex.toString());178 }179 }180 }

是不是很简单.....

源码下载:http://files.cnblogs.com/hongten/QRCodes.rar

读书人网 >编程

热点推荐