writeImageLocal(filePath, createImages(content, imgPath, 120, true));
/**
* 生成新图片
*/
public BufferedImage writeImageLocal(String filePath, BufferedImage img) {
BufferedImage im = null;
if (filePath != null && img != null) {
try {
File outputfile = new File(filePath);
ImageIO.write(img, "jpg", outputfile);
File image = new File(filePath);
im = ImageIO.read(image);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
return im;
}
/**
* 自定义生成二维码尺寸
* @param content
* @param imgPath
* @param needCompress
* @return
* @throws Exception
*/
public static BufferedImage createImages(String content, String imgPath,Integer size,
boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE,size, size, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
MyQRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 插入LOGO
*
* @param source
* 二维码图片
* @param imgPath
* LOGO图片地址
* @param needCompress
* 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath,
boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
log.error(""+imgPath+" 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
分享到:
相关推荐
C#二维码图片生成,二维码图片解码含源代码 比较适合初学和进阶学者,使用的是com.google.zxing.common库 将指定的文字或数字生成二维码图片 将生成的二维码图二内容进行解码成对应的文字或数字。 可进行设置图片的...
二维码 图片生成器,把所有信息制作成一张图片多酷啊
在线二维码图片生成器
用Servlet实现的二维码图片生成,完整打jsp页面,和源代码
此示例是对应用RCODE生成的二维码图片的源码,环境依赖JDK1.6版本的编译器,经实际测试,生成的二维码图片在电脑屏幕上用PDA手持机都能轻松的读取其中的数据,很好用。直接执行QRCodeEncoderTest.java文件就可以运行...
net非常好的二维码图片生成系统源码
.net非常好的二维码图片生成系统源码
java 二维码图片生成+图片转换字符图片。 效果: https://blog.csdn.net/qq_37902949/article/details/81228320 https://blog.csdn.net/qq_37902949/article/details/81228566
c#二维码图片生成/解码,DEMO,完整代码 。visual studio2010
在线二维码图片生成器源码,zxing.net 开发
二维码图片生成器,这个例子实现的是使用 DotNetBarcode.dll 提供的,实现二维码的生成,方便好用。 这个例子实现的是使用Android代码结合Google提供的开源项目ZXing实现二维码的生成,其中包含条形码的生成和解码 ...
python的二维码图片生成 qrcode
该软件基于qrencode进行功能增加,可以生成bmp等常用图片格式的二维码。
二维码图片生成Java代码实例
PHP实例开发源码—php在线二维码图片生成器.zip PHP实例开发源码—php在线二维码图片生成器.zip PHP实例开发源码—php在线二维码图片生成器.zip
PHP实例开发源码——php在线二维码图片生成器
这个是帮同学做完毕业设计后留下的东西,其实也就是到网上收集点资料整合下,不过支持android的资源真的很少,看在辛苦的份上,希望大家可怜点分点我,使用遇到问题的同学也可以联系我,本人专职android开发,有相同...
基于PHP的在线二维码图片生成器.zip
基于PHP的在线二维码图片生成器源码.zip