1、pom文件中引入

<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.2.1</version>
</dependency><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version>
</dependency>

2、编写测试类,目前测试采用的是网页中的图片。可以根据需要更改图片存储地址

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;public class QRTest {public static void main(String[] args) {System.out.println(deEncodeByPath("https://xyz.bj.bcebos.com/test.JPG"));}public static String deEncodeByPath(String path){String content = null;BufferedImage image;URL url = null;try {url = new URL(path);image = ImageIO.read(url);LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map<DecodeHintType,Object> hints = new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET,"UTF-8");Result result = new MultiFormatReader().decode(binaryBitmap,hints);System.out.println("图片中内容 : " + result.getText());content = result.getText();} catch (Exception ex) {ex.printStackTrace();}return content;}}

参考:识别文件中的条形码-zxing_扫描快递单号 zxing_txadf的博客-CSDN博客

MultiFormatReader类,可以同时解析一维/二维的条码,而QRCodeReader类仅能识别二维码。

/*** 根据图片路径解析图片中的条形码,返回Result对象** @param path* @return*/protected Result scanningImage(String path) {if (TextUtils.isEmpty(path)) {return null;}// DecodeHintType 和EncodeHintTypeHashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); //BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true; // 先获取原大小scanBitmap = BitmapFactory.decodeFile(path, options);options.inJustDecodeBounds = false; // 获取新的大小int sampleSize = (int) (options.outHeight / (float) 200);if (sampleSize <= 0)sampleSize = 1;options.inSampleSize = sampleSize;scanBitmap = BitmapFactory.decodeFile(path, options);RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));//仅能识别二维码
//        QRCodeReader reader = new QRCodeReader();MultiFormatReader multiFormatReader = new MultiFormatReader();try {return multiFormatReader.decode(bitmap1, hints);} catch (NotFoundException e) {e.printStackTrace();}return null;}

参考:ZXing应用详解 - 简书 (jianshu.com)

/*** 生成二维码** @param contents 二维码内容* @return 二维码的描述对象 BitMatrix* @throws WriterException 编码时出错*/
private BitMatrix encode(String contents) throws WriterException {final Map<EncodeHintType, Object> hints = new HashMap<>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, "utf-8");return new QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, 320, 320, hints);
}
/*** 解析二维码** @param binaryBitmap 被解析的图形对象* @return 解析的结果*/
private String decode(BinaryBitmap binaryBitmap) {try {Map<DecodeHintType, Object> hints = new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET, "utf-8");hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);Result result = new QRCodeReader().decode(binaryBitmap, hints);return result.getText();} catch (NotFoundException | ChecksumException | FormatException e) {e.printStackTrace();return null;}
}
/*** 解析图片文件上的二维码** @param imageFile 图片文件* @return 解析的结果,null表示解析失败*/
private String decode(File imageFile) {try {BufferedImage image = ImageIO.read(imageFile);LuminanceSource luminanceSource = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(luminanceSource);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map<DecodeHintType, Object> hints = new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET, "utf-8");hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);Result result = new QRCodeReader().decode(binaryBitmap, hints);return result.getText();} catch (Exception e) {e.printStackTrace();return null;}
}
/*** 解析Bitmap中的二维码** @param bitmap* @return 解析结果,null表示解析失败*/
private String decode(Bitmap bitmap) {int width = bitmap.getWidth();int height = bitmap.getHeight();final int[] pixels = new int[width * height];bitmap.getPixels(pixels, 0, width, 0, 0, width, height);RGBLuminanceSource luminanceSource = new RGBLuminanceSource(width, height, pixels);BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));try {final Map<DecodeHintType, Object> hints = new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET, "utf-8");hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);Result result = new QRCodeReader().decode(binaryBitmap, hints);return result.toString();} catch (Exception e) {e.printStackTrace();return null;}
}

参考:java截取图片一部分_java截取图片的一部分_Jinx_Q的博客-CSDN博客

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;import javax.imageio.ImageIO;
public class ImageCutUtils {public static void main(String[] args){cutImg("/Users/jinx/Downloads/21.jpg",500,600,"/Users/jinx/Downloads/24.jpg");}public static void cutImg(String path,int x,int y,String outPath) {//图片路径,截取位置坐标,输出新突破路径try {File sourcePic = new File(path);File outPic = new File(outPath);BufferedImage pic = ImageIO.read(sourcePic);int imgwidth = pic.getWidth();int imgheight = pic.getHeight();int widthInt = imgwidth/4;//截取宽度为原图的四分之一宽int heightInt = imgheight/4;//截取高度为原图的四分之一高//参数依次为,截取起点的x坐标,y坐标,截取宽度,截取高度BufferedImage pic2 = pic.getSubimage(x, y, widthInt, heightInt);//将截取的子图另行存储Image _img = pic2.getScaledInstance(imgwidth, imgheight, Image.SCALE_DEFAULT);BufferedImage image = new BufferedImage(imgwidth, imgheight, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = image.createGraphics();graphics.drawImage(_img, 0, 0, null);graphics.dispose();OutputStream out = new FileOutputStream(outPic);ImageIO.write(image, "jpg", out);out.close();} catch (IOException e) {e.printStackTrace();}}}

java识别二维码-zxing相关推荐

  1. Java 生成二维码 zxing生成二维码 条形码 服务端生成二维码 Java生成条形码

    Java 生成二维码 zxing生成二维码 条形码 服务端生成二维码 Java生成条形码 一.关于ZXing 1.ZXing是谷歌开源的支持二维码.条形码 等图形的生成类库:支持生成.和解码功能. G ...

  2. JAVA生成二维码-zxing

    依赖 <dependency><groupId>com.google.zxing</groupId><artifactId>core</artif ...

  3. Java - 生成二维码图片

    文章目录 生成二维码图片 参考 生成二维码图片 新建 Maven Project,引入依赖: <dependency><groupId>com.google.zxing< ...

  4. Java中识别二维码并且提高二维码的识别率

    我们在Java开发的时候,发现对二维码的识别是不足的.所以我们需要提高识别率. 第一步.识别图片二维码.准备相应的jar包.我们在gradle+idea中开发. compile group: 'com ...

  5. Android利用zxing生成二维码,识别二维码,中间填充图片超详细、超简易教程

    gayhub上的zxing可用于生成二维码,识别二维码 gayhub地址:https://github.com/zxing/zxing 此文只是简易教程,文末附有完整代码和demo下载地址,进入正题: ...

  6. 用 Java 生成和识别二维码就这么简单

    大家好,我是青空. 青空最近一直在思考一个问题,Java 能不能做一些比较有意思的事情,但是在网络上搜索的时候,有意思好玩的东西,都被 Python 给做了.Java 似乎就只剩下八股文,面试,框架, ...

  7. Android利用zxing用相机扫描识别二维码(添加闪光灯和本地二维码)超详细教程

    之前写了怎么用zxing的jar包进行简单的识别和生成二维码,以及生成带图片的二维码. 接下来单独说说怎么用相机扫描二维码,用相机扫描二维码相对于前面的生成二维码,识别二维码来说要麻烦一点,网上的教程 ...

  8. java 生成二维码 QRCode、zxing 两种方式

    版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...

  9. java生成和识别二维码

    全栈工程师开发手册 (作者:栾鹏) java教程全解 本文使用两种方式生成和识别二维码. 方法1:使用日本公司的qrcode 需要引入的包 qrcode_swetake.jar qrcode.jar ...

最新文章

  1. 数据结构--二叉树的创建和相关操作
  2. 一篇来自前端同学对后端接口的吐槽:痛!
  3. int类型的实参与int*类型的形参不兼容_谁告诉的你们Python是强类型语言!站出来,保证不打你!...
  4. async function_理解 Iterator, Generator 和 Async/Await
  5. linux 监控命令
  6. wpf 控件大小随窗体大小改变而改变
  7. linux下GPRS模块的应用程序
  8. 1.8-zabbix服务端安装
  9. python 元类的call_通过 python的 __call__ 函数与元类 实现单例模式
  10. adb查看activity的堆栈信息
  11. 移动医疗的服务怎么做?来听听传统医疗器械厂商怎么说
  12. 平方矩阵——3种思路
  13. 解决g2o无法编译出g2o_viewer的问题
  14. ffmpeg解析出的视频参数PAR,DAR,SAR的意义
  15. 剖析 OceanConnect 物联网特性
  16. Spring实战第五章idea复现
  17. Bouncy Castle 密码包的配置及使用详解
  18. android实现多任务多线程支持断点下载的下载软件
  19. 高级变量类型 ------- 列表、元组、字典
  20. 【操作系统】进程和线程调度

热门文章

  1. 保本≠不亏钱!巴菲特教你如何平衡投资收益与风险
  2. 寻找一起创业的小伙伴
  3. 来吧!是时候升级您的领英技术档案了!
  4. 商城类项目业务简述 可用于面试沟通项目话术 软件测试web项目app项目通用
  5. 从以巴冲突看项目管理在现代战争中的运用
  6. java我的世界物品_我的世界:Java版独享!被隐藏的世界类型,包含各种罕见道具!...
  7. 关于密度函数、分布函数与生存函数的一点看法(一)
  8. Tensorflow GPU版本的安装
  9. ubuntu修改用户密码及开启ssh服务
  10. 计蒜客 2020 蓝桥杯大学 B 组省赛模拟赛(一)