Java实现彩色二维码

步骤:

1.导入google 的zxing.jar包
2.核心方法

//生成二维码的类库
private static MultiFormatWriter mutiWriter = new MultiFormatWriter();
/**
 * @Description: 生成一个彩色二维码
 * @param content 二维码内容
 * @param width 宽度
 * @param height 高度
 * @param path 二维码生成路径
 * @author 
 * @date 2017年11月8日
 */
public static String encode(String content, int width, int height,String path) {
    try {
        String fileName = UUID.randomUUID()+".png";
        File file = new File(path+"/"+fileName);
        ImageIO.write(genBarcode(content, width, height),"png", file);
        return fileName;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

 /**
 * @Description:生成一个二维码
 * @param content  内容
 * @param width 宽
 * @param height 高
 * @return
 * @throws WriterException
 * @throws IOException 
 * @author 
 * @date 2017年12月25日
 */
private static BufferedImage genBarcode(String content, int width, int height) throws WriterException, IOException {
    Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>();
    hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 生成二维码
    BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE,width, height, hint);
    int w = matrix.getWidth();  
    int h = matrix.getHeight();  
    int[] data = new int[w * h]; 
    boolean flag1=true;
    int stopx=0;
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            if(matrix.get(x, y)){
                if(flag1){
                  flag1=false;
                }
            }else{
                if(flag1==false){
                    stopx =x;
                    break;
                }
            }
        } 
        if(flag1==false)
            break;
    } 

    for (int y = 0; y < h; y++) {  
        for (int x = 0; x < w; x++) {  
            if(matrix.get(x, y)){
                if((x<stopx)&&(y<stopx)){
                    Color color = new Color(231, 144, 56);
                    int colorInt = color.getRGB();
                    data[y * width + x] =colorInt;
                }else{
                    int num1 = (int) (50 - (50.0 - 13.0)/ matrix.getHeight()* (y + 1));
                    int num2 = (int) (165 - (165.0 - 72.0) / matrix.getHeight()* (y + 1));
                    int num3 = (int) (162 - (162.0 - 107.0)/ matrix.getHeight() * (y + 1));
                    Color color = new Color(num1, num2, num3);
                    int colorInt = color.getRGB();
                    data[y * w + x] = colorInt;
                }
            }else{
                data[y * w + x] = -1;//白色
            }
        }  
    }  
    BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    image.getRaster().setDataElements(0, 0, width, height, data);
    return image;
}

效果图
这里写图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要识别彩色二维码,可以使用Java中的ZXing库。ZXing是一个流行的开源库,用于识别各种类型的二维码,包括QR码和Data Matrix码等。ZXing库可以在Java中使用,并提供了一个简单的API来读取和解码二维码。 以下是一个简单的Java代码示例,用于识别彩色二维码: ```java import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.NotFoundException; import com.google.zxing.RGBLuminanceSource; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; public class ColorQRCodeReader { public static void main(String[] args) throws IOException, NotFoundException { File file = new File("colorQRCode.png"); BufferedImage image = ImageIO.read(file); int width = image.getWidth(); int height = image.getHeight(); int[] pixels = new int[width * height]; image.getRGB(0, 0, width, height, pixels, 0, width); LuminanceSource source = new RGBLuminanceSource(width, height, pixels); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); MultiFormatReader reader = new MultiFormatReader(); try { Result result = reader.decode(bitmap); System.out.println(result.getText()); } catch (ReaderException e) { e.printStackTrace(); } } } ``` 在这个例子中,我们首先读取彩色二维码图像,然后将其转换为像素数组。接下来,我们使用RGBLuminanceSource类创建一个LuminanceSource对象,该对象包含图像的亮度信息。然后,我们使用HybridBinarizer类创建一个BinaryBitmap对象,该对象可以用于读取和解码二维码。最后,我们使用MultiFormatReader类读取二维码数据,并将其打印到控制台上。 请注意,这个例子假定彩色二维码图像是一个RGB图像。如果彩色二维码图像不是一个RGB图像,则需要相应地调整代码。例如,如果彩色二维码图像是一个CMYK图像,则需要使用CMYKLuminanceSource类代替RGBLuminanceSource类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值