二维码的生成和读取

  

   1.所需Jar包     core-3.3.0.jar,   javase-3.3.0.jar

    

    2.二维码的生成

/**
 * 生成二维码
 * @author Administrator
 *
 */
public class Test {

 public static void main(String[] args) throws WriterException, IOException {
MultiFormatWriter writer=new MultiFormatWriter();

String contents="http://szcet.cn/";
int w=400;
int h=400;
HashMap<EncodeHintType,Object> hits=new HashMap<>();
hits.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix=writer.encode(contents, BarcodeFormat.QR_CODE, w, h,hits);


//创建图片
BufferedImage qr=new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
for(int x=0;x<w;x++){
for(int y=0;y<h;y++){
if(bitMatrix.get(x, y)){
qr.setRGB(x, y, 0xFF0000);
}else{
qr.setRGB(x, y, 0xffffff);
}
}
}
//读取logo
BufferedImage  logimImage=ImageIO.read(new File("d:/cntlogo.png"));
int logoh=50,logow=50;

Image logoUse=logimImage.getScaledInstance(logow, logoh, Image.SCALE_FAST);
   
//2绘制logo
Graphics g1=qr.getGraphics();
int logoX=(w-logow)/2;
int logoY=(h-logoh)/2;
g1.drawImage(logoUse,logoX,logoY,null);

ImageIO.write(qr,"png",new File("d:/2.png"));
//MatrixToImageWriter.writeToPath(bitMatrix, "png", new File("d:/1.png").toPath());
 }


}

   

3.二维码的读取

      public class Test1 {
  

/**
* 二维码的读取
* @param args
*/
public static void main(String[] args)  throws Exception{

//1.读取图片
BufferedImage img=ImageIO.read(new File("D:/2.png"));
//2.转换为亮度数据源
LuminanceSource luminanceSource=new BufferedImageLuminanceSource(img);

//3.使用转换器把亮度数据源转换为二进制位数据
Binarizer binarizer =new HybridBinarizer(luminanceSource);
//4.二进制位数据转换位二进制位图
BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);
//5读取内容
MultiFormatReader formatReader=new MultiFormatReader();
HashMap<DecodeHintType, Object> hints=new HashMap<>();
//hints.put(DecodeHintType.CHARACTER_SET, "utf-8");


Result result=formatReader.decode(binaryBitmap,hints);
String contents=result.getText();
System.out.println(contents);

}
}

   

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值