java实现二维码(带logo)的绘制和解析(zxing by google)
来源:互联网 发布:windows的最新版本 编辑:程序博客网 时间:2024/05/22 15:09
转自:http://blog.csdn.net/typa01_kk/article/details/46673203
二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的;在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理:它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化点。
--百度百科
注:遇到问题:
①:二维码logo图片加入为黑白色,已经解决(测试代码中)。
②:二维码背景色和显示颜色色差大,扫描快,但如果显示颜色设置为黑色外其他颜色,可能无法扫描。
使用到的jar包zxing.jar,源码twodimensioncode,颜色转换:java实现颜色Color对象和16进制之间的转换
1:绘制二维码:
- package com.tsxs.tools.twodimensioncode;
-
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.EnumMap;
-
- import javax.imageio.ImageIO;
-
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
-
-
-
-
-
- public final class EncodeImgZxing {
-
- private static final int BLACK = 0xFF000000;
-
- private static final int WHITE = 0xFFFFFFFF;
-
-
- private static final int width = 300;
-
- private static final int height = 300;
-
- private static final EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
- static{
-
-
-
-
-
-
-
-
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
-
- hints.put(EncodeHintType.MARGIN, 1);
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- hints.put(EncodeHintType.MAX_SIZE, 350);
- hints.put(EncodeHintType.MIN_SIZE, 150);
- }
-
-
-
-
-
- public static BufferedImage encodeImg(String contents){
- BufferedImage image = null;
- try{
- BitMatrix matrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
- image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- int width = matrix.getWidth();
- int height = matrix.getHeight();
- for(int x = 0; x < width; x++){
- for(int y =0;y < height; y++){
- image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
- }
- }
- }catch(Exception e){
- System.out.println("生成二维码失败"+e.getMessage());
- }
- return image;
- }
-
-
-
-
-
-
-
- public static void writeToFile(String contents,String format,File file){
- BufferedImage image = encodeImg(contents);
- try {
- ImageIO.write(image, format, file);
- } catch (IOException e) {
- System.out.println("二维码写入文件失败"+e.getMessage());
- }
- }
-
-
-
-
-
-
- public static void writeToStream(String contents,String format,OutputStream stream){
- BufferedImage image = encodeImg(contents);
- try {
- ImageIO.write(image, format, stream);
- } catch (IOException e) {
- System.out.println("二维码写入流失败"+e.getMessage());
- }
- }
- }
2:测试代码:
- package com.tsxs.test;
-
- import java.io.File;
-
- import org.junit.Test;
-
- import com.tsxs.tools.twodimensioncode.DecodeImgZxing;
- import com.tsxs.tools.twodimensioncode.EncodeImgZingLogo;
- import com.tsxs.tools.twodimensioncode.EncodeImgZxing;
-
- public class EncodeImgZingLogoTest {
-
- @Test
- public void testWriteToFile() {
- String contents = "http://blog.csdn.net/typa01_kk";
- String format = "jpeg";
-
- File logoImg = new File("D:"+File.separator+"logo.jpg");
- File img = new File("D:"+File.separator+"csdn.jpg");
- EncodeImgZxing.writeToFile(contents, format, img);
-
- File img1 = new File("D:"+File.separator+"csdnlogo.jpg");
- EncodeImgZingLogo.writeToFile(img, logoImg, format, img1);
-
-
- String content = DecodeImgZxing.decodeImg(img);
- System.out.println("1:"+content);
-
- String content1 = DecodeImgZxing.decodeImg(img1);
- System.out.println("2:"+content1);
- }
-
- }
效果图为:img1:(H 1) img2(M 4) img3(color change ,无法扫描,红色改为黑色,可扫描,速度慢)
3:二维码绘制logo(二维码和logo图片合并绘制,图片尽量为方形,有利扫描和显示)
- package com.tsxs.tools.twodimensioncode;
-
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
-
- import javax.imageio.ImageIO;
-
-
-
-
-
- public final class EncodeImgZingLogo {
-
-
-
-
-
- public static BufferedImage encodeImgLogo(File twodimensioncodeImg,File logoImg){
- BufferedImage twodimensioncode = null;
- try{
- if(!twodimensioncodeImg.isFile() || !logoImg.isFile()){
- System.out.println("输入非图片");
- return null;
- }
-
- twodimensioncode = ImageIO.read(twodimensioncodeImg);
-
- Graphics2D g = twodimensioncode.createGraphics();
-
- BufferedImage logo = ImageIO.read(logoImg);
-
- int logoWidth = logo.getWidth(null) > twodimensioncode.getWidth()*2 /10 ? (twodimensioncode.getWidth()*2 /10) : logo.getWidth(null);
- int logoHeight = logo.getHeight(null) > twodimensioncode.getHeight()*2 /10 ? (twodimensioncode.getHeight()*2 /10) : logo.getHeight(null);
-
-
- int x = (twodimensioncode.getWidth() - logoWidth) / 2;
- int y = (twodimensioncode.getHeight() - logoHeight) / 2;
-
-
-
-
- g.drawImage(logo, x, y, logoWidth, logoHeight, null);
- g.drawRoundRect(x, y, logoWidth, logoHeight, 15 ,15);
-
- g.setStroke(new BasicStroke(2));
-
- g.setColor(Color.WHITE);
- g.drawRect(x, y, logoWidth, logoHeight);
- g.dispose();
- logo.flush();
- twodimensioncode.flush();
- }catch(Exception e){
- System.out.println("二维码绘制logo失败");
- }
- return twodimensioncode;
- }
-
-
-
-
-
-
-
-
- public static void writeToFile(File twodimensioncodeImg,File logoImg,String format,File file){
- BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);
- try {
- ImageIO.write(image, format, file);
- } catch (IOException e) {
- System.out.println("二维码写入文件失败"+e.getMessage());
- }
- }
-
-
-
-
-
-
-
- public static void writeToStream(File twodimensioncodeImg,File logoImg,String format,OutputStream stream){
- BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);
- try {
- ImageIO.write(image, format, stream);
- } catch (IOException e) {
- System.out.println("二维码写入流失败"+e.getMessage());
- }
- }
- }
使用上测试代码产生二维码为:
4:二维码内容解析:
- package com.tsxs.tools.twodimensioncode;
-
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.EnumMap;
-
- import javax.imageio.ImageIO;
-
- import com.google.zxing.Binarizer;
- 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 final class DecodeImgZxing {
-
- private static final EnumMap<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
- static{
- hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
- }
-
-
-
-
-
- public static String decodeImg(File imgFile){
- String content = null;
- if(!imgFile.isFile()){
- System.out.println("输入非文件");
- return null;
- }
- try {
- BufferedImage image = ImageIO.read(imgFile);
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- Binarizer binarizer = new HybridBinarizer(source);
- BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
- MultiFormatReader reader = new MultiFormatReader();
- Result result = reader.decode(binaryBitmap, hints);
- content = result.getText();
-
- } catch (NotFoundException e) {
- System.out.println("二维码解析NotFoundException");
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("二维码解析IOException");
- e.printStackTrace();
- }
- return content;
- }
- }
使用测试代码,测试结果:
1:http://blog.csdn.NET/typa01_kk
2:http://blog.csdn.Net/typa01_kk
0 0