使用java解析和制作二维码
2014-07-09 11:52
239 查看
项目结构
文件源码 QR.zip
第一步:导入zxing的两个架包 core.jar和javase.jar
第二步:使用工具类 MatrixToImageWriter.java
第三步:创建二维码
第四步:解析二维码
文件源码 QR.zip
第一步:导入zxing的两个架包 core.jar和javase.jar
第二步:使用工具类 MatrixToImageWriter.java
package util; import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.awt.image.BufferedImage; public final class MatrixToImageWriter { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() { } public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }
第三步:创建二维码
package demo; import java.io.File; import java.util.Hashtable; import util.MatrixToImageWriter; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; /** *将文字转换为二维码 */ public class Encoder { public static void main(String[] args) throws Exception { String text = "http://www.tfgzs.com"; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); // 内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, 300, 300, hints); // 生成二维码 MatrixToImageWriter.writeToFile(bitMatrix, "gif", new File("E:/123/new.gif")); System.out.println("二维码生成完成"); } }
第四步:解析二维码
package demo; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; /** * 将二维码解析为文字 */ public class Decoder { public static void main(String[] args) { BufferedImage bufferedImage = null; try { bufferedImage = ImageIO.read(new File("E://123/qrcodeImage.png")); } catch (IOException e) { e.printStackTrace(); } LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "GBK"); Result result = null; try { result = new MultiFormatReader().decode(bitmap, hints); } catch (NotFoundException e) { e.printStackTrace(); } System.out.println(result.toString()); } }
相关文章推荐
- Java使用Zxing生成、解析二维码工具类
- Java使用QRCode.jar生成与解析二维码
- Java使用Zxing生成、解析二维码工具类
- java二维码生成 使用SSM框架 搭建属于自己的APP二维码合成、解析、下载
- QRCode-使用Java生成以及解析二维码
- Java使用QRCode.jar生成与解析二维码
- Android及Java使用google.zxing生成和解码(解析)二维码(QRCode)
- java二维码生成 使用SSM框架 搭建属于自己的APP二维码合成、解析、下载
- Java使用谷歌的zxing生成二维码Qrcode并解析提取二维码图片内容
- java使用zxing生成和解析二维码
- Java使用QRCode.jar生成与解析二维码
- java完整的利用itext7制作pdf、二维码图片插入pdf,并解析pdf中的二维码信息
- java实现二维码-使用QR Code方式生成和解析二维码
- 解析Java中的定时器及使用定时器制作弹弹球游戏的示例
- 使用 Java 生成二维码图像,解析二维码图像.
- java解析xml之三:使用domj4
- java解析xml之一:使用SAX与DOM
- Java中使用DOM来解析xml文档
- java解析xml之二:使用JDOM
- java解析xml之一:使用SAX与DOM