- 浏览: 183620 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
arpenker:
这个命令行运行的前提是要基于LINUX服务器能够自己手动安装F ...
linux java转amr为mp3 -
smith789:
使用的类都废弃了
HttpUtil工具类 -
s21109:
自定义TabHost之手势切换Tab视图(不带动画)
Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码
1、二维码的生成
1.1 将Zxing-core.jar 包加入到classpath下。
1.2 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。
1.3 编写生成二维码的实现代码
现在运行后即可生成一张二维码图片,是不是很简单啊? 接下来我们看看如何解析二维码
2、二维码的解析
2.1 将Zxing-core.jar 包加入到classpath下。
2.2 和生成一样,我们需要一个辅助类( BufferedImageLuminanceSource),同样该类Google也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦
2.3 编写解析二维码的实现代码
现在运行后可以看到控制台打印出了二维码的内容。
到此为止,利用Zxing生成和解析二维码就讲述演示完毕,主要为自己做备忘,同时方便有需要的人。呵呵!
1、二维码的生成
1.1 将Zxing-core.jar 包加入到classpath下。
1.2 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。
import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO; import java.io.File; import java.io.OutputStream; import java.io.IOException; import java.awt.image.BufferedImage; public final class MatrixToImageWriter { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private MatrixToImageWriter() {} public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }
1.3 编写生成二维码的实现代码
try { String content = "120605181003;http://www.cnblogs.com/jtmjx"; String path = "C:/Users/Administrator/Desktop/testImage"; MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints); File file1 = new File(path,"餐巾纸.jpg"); MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1); } catch (Exception e) { e.printStackTrace(); }
现在运行后即可生成一张二维码图片,是不是很简单啊? 接下来我们看看如何解析二维码
2、二维码的解析
2.1 将Zxing-core.jar 包加入到classpath下。
2.2 和生成一样,我们需要一个辅助类( BufferedImageLuminanceSource),同样该类Google也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦
BufferedImageLuminanceSource import com.google.zxing.LuminanceSource; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; public final class BufferedImageLuminanceSource extends LuminanceSource { private final BufferedImage image; private final int left; private final int top; public BufferedImageLuminanceSource(BufferedImage image) { this(image, 0, 0, image.getWidth(), image.getHeight()); } public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) { super(width, height); int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); if (left + width > sourceWidth || top + height > sourceHeight) { throw new IllegalArgumentException("Crop rectangle does not fit within image data."); } for (int y = top; y < top + height; y++) { for (int x = left; x < left + width; x++) { if ((image.getRGB(x, y) & 0xFF000000) == 0) { image.setRGB(x, y, 0xFFFFFFFF); // = white } } } this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY); this.image.getGraphics().drawImage(image, 0, 0, null); this.left = left; this.top = top; } @Override public byte[] getRow(int y, byte[] row) { if (y < 0 || y >= getHeight()) { throw new IllegalArgumentException("Requested row is outside the image: " + y); } int width = getWidth(); if (row == null || row.length < width) { row = new byte[width]; } image.getRaster().getDataElements(left, top + y, width, 1, row); return row; } @Override public byte[] getMatrix() { int width = getWidth(); int height = getHeight(); int area = width * height; byte[] matrix = new byte[area]; image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; } @Override public boolean isCropSupported() { return true; } @Override public LuminanceSource crop(int left, int top, int width, int height) { return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height); } @Override public boolean isRotateSupported() { return true; } @Override public LuminanceSource rotateCounterClockwise() { int sourceWidth = image.getWidth(); int sourceHeight = image.getHeight(); AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth); BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g = rotatedImage.createGraphics(); g.drawImage(image, transform, null); g.dispose(); int width = getWidth(); return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width); } }
2.3 编写解析二维码的实现代码
try { MultiFormatReader formatReader = new MultiFormatReader(); String filePath = "C:/Users/Administrator/Desktop/testImage/test.jpg"; File file = new File(filePath); BufferedImage image = ImageIO.read(file);; LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); Result result = formatReader.decode(binaryBitmap,hints); System.out.println("result = "+ result.toString()); System.out.println("resultFormat = "+ result.getBarcodeFormat()); System.out.println("resultText = "+ result.getText()); } catch (Exception e) { e.printStackTrace(); }
现在运行后可以看到控制台打印出了二维码的内容。
到此为止,利用Zxing生成和解析二维码就讲述演示完毕,主要为自己做备忘,同时方便有需要的人。呵呵!
发表评论
-
Linux tomcat8,JDK8 启动卡住半天 后才能正常启动解决方案
2017-09-05 16:06 698今天在服务器上部署网站时 启动tomcat无错 tail -f ... -
windows下安装redis
2016-07-25 18:30 459windows下安装redis 1、redis简介 redi ... -
解决ScrollView中嵌套ListView或RecycleView卡顿问题
2016-01-20 02:23 1912解决问题:<listview上下滑动卡顿> 只要 ... -
解决Eclipse启动速度过慢的问题
2016-01-13 20:20 2450Delete the following directory: ... -
Android,iOS打开手机QQ与指定用户聊天界面
2016-01-07 18:19 1737在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服 ... -
Android Studio debug使用release的签名
2015-12-09 17:50 1239当我们在做微信微博sdk分享的时候调试非常麻烦,因为要使用对应 ... -
mysql根据经纬度查询附近信息
2015-12-08 23:10 1481#查询偏差经纬度1度(111公里)范围内,按距离排序,获得最近 ... -
一个tomcat配置多个端口
2015-10-22 02:13 476http://linder.iteye.com/blog/78 ... -
解决Android SDK Manager更新、下载速度慢
2015-10-12 16:39 606转自: [urlhttp://blog.163.com/bob ... -
android httpClient 支持HTTPS的2种处理方式
2015-09-11 17:37 1297http://my.oschina.net/blackylin ... -
带进度条的WebView
2015-08-29 16:53 584public class ProgressWebView ex ... -
Android中javascript和java互相调用
2015-08-29 16:06 574http://www.jb51.net/article/599 ... -
仿微信朋友圈点击图片变暗
2015-08-20 22:35 998import android.content.Context; ... -
Android图片缓存框架Fresco极其强大的配置
2015-07-20 21:12 1286Android图片缓存框架Fresco极其强大的配置,配置强大 ... -
Android实现ViewPager+RadioGroup滑动效果
2015-07-19 15:05 1194有滚动标识的滑动条,顺畅无比哦 -
Android实现可用性非常高的滚动数字条
2015-07-18 15:48 5042对外提供接口,可以是调用并改变数字滚动条的样式 -
Java实现二维码生成和解析代码
2015-07-05 16:36 1011请下载附件,查看代码 -
Android-webview和js脚本语言交互的时候怎么获取js方法的返回值
2015-07-02 12:10 2067http://zhidao.baidu.com/questio ... -
JS判断手机端是否安装了某个客户端APP应用
2015-05-15 17:02 3299最近在做项目的wap版,有个需求就是,先判断手机上是否有我们的 ... -
Android选图裁剪实现
2015-04-29 18:22 490看附件顶顶顶顶顶
相关推荐
谷歌二维码jar包(zxing-3.3.2 && zxing-core.2.1 && zxing-javase-2.1)
二维码绘图jar包-zxing.jar-core.jar-zxing-javase.jar 亲测能用
google最新的二维码生成zxing-3.2.1.jar只支持jdk1.7以上,但考虑到还有很多人使用jdk1.6,特地更改了源码中jdk1.7以上才有的语法以适应jdk1.6 已经包含core 与javase两个包,把这两个重要的包打包到一个jar中了,完美...
二维码扫描开源项目Zxing-core代码jar包,版本是:3.4.1。核心包为core-3.4.1-SNAPSHOT.jar,还包含了core-3.4.1-SNAPSHOT_proguard_base.jar,core-3.4.1-SNAPSHOT-javadoc.jar,core-3.4.1-SNAPSHOT-sources.jar
zxing-3.3.0,java开发二维码的jar包,包含core和javase下的内容,可分别调用其中方法生成二维码并输出到文件
一维码二维码所需资源jar包,包含javase-3.2.1.jar 和 zxing-core-3.2.1.jar
android 二维码zxing库,用于生成二维码和二维码扫描
代码学习来源为网络,只添加core中的com包和javase中的com包生成的jar包存在错误,缺少jcommander相关文件。这里发的文件可直接使用。附带测试代码(源码使用时倒数第七行必须修改存放地址!!!!)。
ZXing-core-3.3.0.jar,二维码生成、google开源项目。maven依赖 <groupId>com.google.zxing</groupId> <artifactId>core <version>3.3.0</version>
zxing2.2版本jar包,分别是core-2.2.jar和javase-2.2.jar,java生成二维码的两个jar,core为二维码生成包,javase为java文件什么的工具类包。支持jdk 1.6 亲身测试
利用Google的zxing-master实现二维码扫描。 Android 基于google Zxing实现对手机中的二维码进行扫描。
生成二维码工具类,ZXing的jar文件,可以用来做扫码和生成二维码相关的操作(ZXing's jar file can be used to do scan code and generate two-dimensional code related operations.)
zxing jar包 二维码Java开发包 Android开发条形码,二维码
QR二维码包,zxing.jar 包,最新版core-3.3.3.jar,QR二维码3.3.3 版本,Java生成二维码。
zxing core-3.2.1.jar二维码jar包 core.二维码,识别,扫描
生成二维码时使用的Zxingjar包,可以生成二维码,可以生成带logo的二维码,快来试试吧~生成属于你自己的二维码
Google ZXing开源项目可以制作和扫描解析条形码、二维码 ZXing2.3.0的核心core包
zxing-core-2.3 ,安卓开发二维码专用,源代码生成的jar包
Zxing这个开源项目是用Java实现的,用手机自带的摄像头,扫描条形码/二维码,不需要和服务器通信(当然这个项目也可以用于PC和服务器),就可以做一维或二维码的识别。2019-08-28手动编译。
google zxing生成二维码两个必备jar包 包含 zxing-core.2.1.jar zxing-javase-2.1.jar 两个文件