ZXing二维图

public class TestQrcode {
	
	/**
	 * @introduction 设置颜色值,由于后来的setRGB方法
	 * @author Guo
	 */
	private static final int BLACK = 0xff000000;
	private static final int WHITE = 0xFFFFFFFF;


	public static void main(String[] args) {
		
		TestQrcode test = new TestQrcode();
		File file = new File("G://Picture//qrcode.png");
		String str = "很好";
		
		try {
			test.encode(new String(str.getBytes("UTF-8"), "ISO-8859-1"), file, BarcodeFormat.QR_CODE, 200, 200);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		/**
		 * @introduction 解析二维码图片的信息
		 * @author Guo
		 */
		//test.decode(file);

	}
	
	public void encode(String contents, File file, BarcodeFormat format, int width, int height) {
		
		try {
			/**
			 * @introduction BitMatrix是构建二维矩阵位图的类
			 * @introduction 通过MultiFormatWriter类编码二维码的格式
			 * @author Guo
			 */
			BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
			writeToFile(bitMatrix, "png", file);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
		
		/**
		 * @introduction BufferedImage描述具有可访问图像数据缓冲区的 Image
		 * @author Guo
		 */
		BufferedImage image = toBufferedImage(matrix);
		
		/**
		 * @introduction ImageIO该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法
		 * @introduction write方法使用支持给定格式的任意 ImageWriter 将一个图像写入 File
		 * @author Guo
		 */
		ImageIO.write(image, format, file);
	}
	
	public BufferedImage toBufferedImage(BitMatrix bitMatrix) {
		
		/**
		 * @introduction 拿到位图的基本数据
		 * @author Guo
		 */
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		
		/**
		 * @introduction 构造一个类型为预定义图像类型之一的 BufferedImage
		 * @author Guo
		 */
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		
		/**
		 * @introduction 设置黑白位点
		 * @author Guo
		 */
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) == true ? BLACK : WHITE);
			}
		}
		return image;
	}
	
	public void decode(File file) {
		
		BufferedImage image;
		
		try {
			image = ImageIO.read(file);
			
			if(image == null)
				System.out.println("Can not find the image!");
			else {
				
				LuminanceSource source = new BufferedImageLuminanceSource(image);
				BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
				
				Result result;
				Hashtable hints = new Hashtable();
				hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
				result = new MultiFormatReader().decode(bitmap, hints);
				String resultStr = result.getText();
				System.out.println("解析后内容:" + resultStr);

			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值