Zxing.jar生成带有logo的二维码
来源:互联网 发布:网络医托 编辑:程序博客网 时间:2024/04/30 08:29
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
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.WriterException;
import com.google.zxing.common.BitMatrix;
public class QRCode
{
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static final int WIDTH = 30;
private static final int HEIGHT = 30;
private QRCode(){};
/**
* drawQrcode(创建二维码通用方法)
* @param matrix
* @param logoPath logo文件路径
* @Exception IOException
*/
public static BufferedImage drawQrcode(BitMatrix matrix, String logoPath) throws IOException
{
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);
}
}
insertImage(image, logoPath, true);
return image;
}
/**
* drawQrcode(创建二维码通用方法)
* @param matrix
* @param logoPath logo文件路径
* @param needCompress 是否需要压缩logo
* @Exception IOException
*/
public static BufferedImage drawQrcode(BitMatrix matrix, String logoPath, boolean needCompress) throws IOException
{
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);
}
}
insertImage(image, logoPath, needCompress);
return image;
}
/**
* drawQrcode(创建二维码通用方法)
* @param matrix
* @param logoPath logo文件路径
* @param format 文件格式
* @param file 生成的二维码文件
* @Exception IOException
*/
public static void writeToFile(BitMatrix matrix, String logoPath, String format, File file) throws IOException
{
BufferedImage image = drawQrcode(matrix, logoPath);
if(!ImageIO.write(image, format, file))
{
throw new IOException("could not write an image of format"+format+" to file"+file);
}
}
/**
* drawQrcode(创建二维码通用方法)
* @param matrix
* @param logoPath logo文件路径
* @param format 文件格式
* @param stream 生成的二维码文件流
* @Exception IOException
*/
public static void writeToStream(BitMatrix matrix, String logoPath, String format, OutputStream stream) throws IOException
{
BufferedImage image = drawQrcode(matrix, logoPath);
if(!ImageIO.write(image, format, stream))
{
throw new IOException("could not write an image of format"+format);
}
}
/**
* insertImage(向二维码中插入logo)
* @param image BufferedImage对象
* @param logoPath logo文件路径
* @param needCompress 是否需要压缩logo
*/
private static void insertImage(BufferedImage image, String logoPath, boolean needCompress) throws IOException
{
File file = new File(logoPath);
if(!file.exists())
{
System.err.println("该文件不存在"+logoPath);
}
Image src = ImageIO.read(file);
int width = src.getWidth(null);
int height = src.getHeight(null);
if(needCompress)
{
if(width > WIDTH)
{
width = WIDTH;
}
if(height > HEIGHT)
{
height = HEIGHT;
}
}
Image img = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics gs = bufImg.getGraphics();
gs.drawImage(img, 0, 0, null);
gs.dispose();
src = img;
Graphics2D gs2d = image.createGraphics();
int x = (image.getWidth() - width)/2;
int y = (image.getHeight() - height)/2;
gs2d.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, height, 6, 6);
gs2d.setStroke(new BasicStroke(3f));
gs2d.draw(shape);
gs2d.dispose();
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws WriterException, IOException
{
String text = "然而《读者文摘》的发展并非一帆风顺。1982年初,美国《读者文摘》来函指出:甘肃人民出版社使用《读者文摘》中文" +
"名是不正当的,不符合国际版权协定。中美两家《读者文摘》长达数年的版权之争从此开始。从1990年开始,中美两家"+
"《读者文摘》的版权之争再次升级。1992年美国《读者文摘》委托律师致函中国《读者文摘》,要求停止使用中文商标《读者文摘》。" +
"后为化解与美国《读者文摘》发生的版权纠纷,1993年3月号《读者文摘》刊登征名启事,在读者中引起强烈反响,共收到应征信十万多封。" +
"信中提到新名有:读者、读友、读者之家、谈天说地、共享等等。最后使用了《读者》一名。1993年第七期,《读者文摘》正式改名为《读者》。";
int width = 300;
int height = 300;
String format = "jpg";
String logoPath = "G:/学习/桌面文件备份/123.jpg";
Hashtable table = new Hashtable();
table.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, table);
File file = new File("d:"+File.separator+"qrcode.jpg");
QRCode.writeToFile(matrix, logoPath, format, file);
}
}
- Zxing.jar生成带有logo的二维码
- 使用Zxing生成带有logo的二维码
- 用Zxing来生成带有logo图的二维码
- 使用Zxing生成带logo的二维码
- Java:生成带logo的二维码(ZXing)
- Java生成中间带有logo的二维码
- Android 生成带有Logo的二维码
- js生成带有图片logo的二维码:
- Zxing生成含有LOGO二维码
- 二维码及带logo二维码的生成和解析-Zxing
- ZXing生成二维码和带logo的二维码
- zxing 二维码、带logo二维码生成
- ZXing生成条形码、二维码、带logo二维码
- ZXing生成和解析带LOGO的二维码
- android 使用zxing生成中间嵌套logo的二维码
- Android利用ZXing生成带LOGO的二维码图片
- java使用zxing生成带logo的二维码图片
- java使用zxing生成带logo的二维码图片
- 黄芪桂圆枸杞红枣茶的作用
- mvn编译时下载源代码
- Java多线程(2)--线程的中断和中断的控制
- Oracle中快速删除某个用户下的所有表数据 禁用所有外键
- SOCKET, TCP/UDP, HTTP, FTP
- Zxing.jar生成带有logo的二维码
- 一、阻塞非阻塞与同步异步
- 创建动态数组
- 小希的迷宫 hdu 1272
- 数组的关系运算
- 安卓定义一根很小的竖线及自定义圆角
- C++中const成员变量的初始化
- 三种方法解决android帮助文档打开慢
- 服务器安全维护