package com.util;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.logo.zxing.MatrixToImageWriterEx;
import com.logo.zxing.MatrixToLogoImageConfig;
public class TwoCodeUtil {
/*
引用的类库或包等:使用第三方开源工具类库com.google.zxing来实现二维码(QRCode)及一维码(条形码)的生成和解析
函数名称:生成二维码
功能/参数说明:生成和解析二维码及一维码
版本信息:
Ver() Date Author Description
--------- ---------- --------------- ------------------------------------
1.0 2013/12/3 yejj 1. Created this Function.
备注:
使用范例:
运行test包里的Test.java,生成和解析二维码及一维码
*/
private static final String CODE = "utf-8";
private static final int BLACK = 0xff000000;
private static final int WHITE = 0xFFFFFFFF;
public static String TwoCode(String content,String format,int width,int height,String savepath) throws Exception{
String result="异常";
if(width>0&&height>0){
BitMatrix matrix = MatrixToImageWriterEx.createQRCode(content,width, height);
MatrixToImageWriterEx.writeToFile(matrix, format,savepath);
result="成功:文件位于"+savepath;
}else{
result="长度或高度不能小于0";
}
return result;
}
//生成带LOGO的
public static String TwoCode(String content,String format,int width,int height,String savepath,Color borderColor,String logopath) throws Exception{
String result="异常";
if(width>0&&height>0){
BitMatrix matrix = MatrixToImageWriterEx.createQRCode(content,width, height);
MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(borderColor, 4);
MatrixToImageWriterEx.writeToFile(matrix,format,savepath,logopath,logoConfig);
//默认的
result="成功:文件位于"+savepath;
}else{
result="长度或高度不能小于0";
}
return result;
}
/**
* 生成一维码(128)
*
* @author yejj
* @param str
* @param width
* @param height
* @return
*/
public static BufferedImage getBarcode(String content, Integer width,
Integer height)
{
if (width == null || width < 200)
{
width = 200;
}
if (height == null || height < 50)
{
height = 50;
}
try
{
// 文字编码
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, CODE);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.CODE_128, width, height, hints);
return toBufferedImage(bitMatrix);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* 生成一维码,写到文件中
*
* @author wuhongbo
* @param str
* @param height
* @param file
* @throws IOException
*/
public static void getBarcodeWriteFile(String str, Integer width,
Integer height, File file) throws IOException
{
BufferedImage image = getBarcode(str, width, height);
ImageIO.write(image, "png", file);
}
/**
* 转换成图片
*
* @author wuhongbo
* @param matrix
* @return
*/
private static BufferedImage toBufferedImage(BitMatrix matrix)
{
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
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 String read(File file)
{
BufferedImage image;
try
{
if (file == null || file.exists() == false)
{
throw new Exception(" File not found:" + file.getPath());
}
image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
// 解码设置编码方式为:utf-8,
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
生成和解析二维码(可带图片)及一维码
需积分: 10 80 浏览量
2013-12-16
15:08:50
上传
评论
收藏 927KB RAR 举报
Joker_Ye
- 粉丝: 1897
- 资源: 26
最新资源
- JSP-JTBC-CMS(SQLITE).rar
- MC3362和MC145151调频无线接收器的设计.pdf
- MiniRenamer-v100.0一款简单易用的批量文件重命名工具(已注册PRO版本).rar
- 小狐狸Ai系统 小狐狸ai付费创作系统V2.8.0 ChatGPT智能机器人
- 公孙离-内衣-肚兜.zipgsl
- 快慢指针判断链表是否有环-go 语言实现
- 学生成绩管理系统的设计与实现-收藏备用.pdf
- JSP+SQL网站流量统计管理系统(源代码+论文).rar
- IBM-PC-XT微机过程...道中模拟量数据的采集和处理.pdf
- JSP+SQL网上选课系统(源代码+论文+答辩PPT).rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈