import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
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;
public class ZxingUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* @param url 二维码content
* @param width
* @param height
* @param flag
* @return
* @throws Exception
*/
public static BitMatrix buildBitMatrix(String url,int width,int height,boolean flag) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//内容所使用编码
BitMatrix bitMatrix = new MultiFormatWriter().encode(url,BarcodeFormat.QR_CODE, width, height, hints);
return flag?deleteWhite(bitMatrix):bitMatrix;
}
/**
* 保存二维码图片
* @param bitMatrix 二维码
* @param format 保存格式
* @param filepath 保存路径
* @param picname 保存name
* @throws Exception
*/
public static void creatTwoCode(BitMatrix bitMatrix,String format,String filepath,String picname) throws Exception {
File outputFile = new File(filepath+File.separator+picname+"."+format);//生成二维码图片
writeToFile(bitMatrix, format, outputFile);
}
public static BitMatrix deleteWhite(BitMatrix matrix){
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}
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);
}
}
}
分享到:
相关推荐
谷歌二维码jar包(zxing-3.3.2 && zxing-core.2.1 && zxing-javase-2.1)
利用Google的zxing-master实现二维码扫描。 Android 基于google Zxing实现对手机中的二维码进行扫描。
java实现二维码的绘制和解析(zxing by google) 截止目前为止最新版本为1.7,提供以下编码格式的支持: UPC-A and UPC-E EAN-8 and EAN-13 Code 39 Code 93 Code 128 QR Code ITF Codabar RSS-14 (all ...
zxing-master,zxing.jar 二维码/条形码扫描器/生成器工具类源码
zxing jar包 二维码Java开发包 Android开发条形码,二维码
zxing-3.3.0,java开发二维码的jar包,包含core和javase下的内容,可分别调用其中方法生成二维码并输出到文件
java实现二维码([带]logo)的绘制和解析(zxing by google)
google最新的二维码生成zxing-3.2.1.jar只支持jdk1.7以上,但考虑到还有很多人使用jdk1.6,特地更改了源码中jdk1.7以上才有的语法以适应jdk1.6 已经包含core 与javase两个包,把这两个重要的包打包到一个jar中了,完美...
Android实现二维码扫描功能(四)-ZXing识别图片二维码,从相册中选择图片并识别图中二维码,详见:http://blog.csdn.net/ahuyangdong/article/details/77487650,持续更新的版本:...最新的lib版本:...
谷歌开发的操作二维码的工具包,包括二维码的生成与解析,欢迎下载
本sample是android系统使用zxing进行条形码、二维码编解码处理的范例,是google提供范例的精简版本,仅保留扫码及二维码生成。本精简版本来源于网络,只是替换zxing的核心库到最新版,并增加了一些注释。 zxing简介 ...
google zxing生成二维码两个必备jar包 包含 zxing-core.2.1.jar zxing-javase-2.1.jar 两个文件
google zxing-3.3.0.jar 包下载,java生成二维码google zxing Jar包,网络不好的同学拿去用了。
zxing是google开源的二维码生成和解析工具,可用于j2me和android项目中,2.1是最新版的。
第三方ZXing库zxing-android-embedded使用及自定义 1.可通过Intent使用(只需少量代码) 2.可以轻松集成到Activity中,使用方便,只需几行代码轻松搞定 3.可以在横屏或竖屏模式下执行扫描 4.摄像头在后台线程中进行...
1.ZXing 使用版本为 2.2 2.lib文件夹下的zxing-2.2-client.jar和zxing-2.2-core.jar 为依据 ZXing-2.2.zip 中的core包和javase包的源码导出的jar ...7.有ZXing-2.2.zip源码包,官方google网站下载的。
使用google-zxing 生成二维码的简单小实例,包含zxing相关jar 包。可直接复制测试使用。
google提供二维码生成工具包Zxing-javase.jar
适用于javaee开发 Google官方ZXing源码,作为二维码条形码的扫码器的框架
GoogleZxing的jar包们,二维码的开发必备!