二维码融合模板

二维码融合图片

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.skjd.service.system.driver.DriverService; public class ImageAndQRcode { @Autowired
DriverService driverService; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; ImageAndQRcode() { } private 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;
} private 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);
}
} /**      * @Title: 构造图片      * @Description: 生成水印并返回java.awt.image.BufferedImage      * @param file源文件(图片)      * @param waterFile水印文件(图片)      * @param x距离右下角的X偏移量      * @param y 距离右下角的Y偏移量      * @param alpha 透明度, 选择值从0.0~1.0: 完全透明~完全不透明      * @return BufferedImage      * @throws IOException      */
private static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException { Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());
BufferedImage image=BufferedImageBuilder.toBufferedImage(src); Image src1=Toolkit.getDefaultToolkit().getImage(waterFile.getPath());
BufferedImage image1=BufferedImageBuilder.toBufferedImage(src1); Graphics2D g2d = image.createGraphics(); int waterImgWidth = image1.getWidth();// 获取层图的宽度 int waterImgHeight = image1.getHeight();// 获取层图的高度 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g2d.drawImage(image1, x, y, waterImgWidth, waterImgHeight, null); g2d.dispose();// 释放图形上下文使用的系统资源 return image;
} /**
* 输出水印图片
* @param buffImg 图像加水印之后的BufferedImage对象
* @param savePath 图像加水印之后的保存路径
*/
private void generateWaterFile(BufferedImage buffImg, String savePath) { int temp = savePath.lastIndexOf(".") + 1; try { ImageIO.write(buffImg, savePath.substring(temp), new File(savePath)); } catch (IOException e1) { e1.printStackTrace();
} } /**
     * 
     * @param text 二维码内容
     * @param width 二维码图片宽度
     * @param height 二维码图片高度
     * @param format 二维码的图片格式
     * @param sourceFilePath 底层图片路径
     * @param waterFilePath 二维码路径
     * @param saveFilePath 合成图片路径
     * @param maginx  二维码距离底图x轴距离
     * @param maginy  二维码距离底图y轴距离
     * @throws Exception
    
* @return */
public static byte[] addImageQRcode(String text, int width, int height, String format, String sourceFilePath,
String waterFilePath, String saveFilePath, int maginx, int maginy) throws Exception { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码 BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); File outputFile = new File(waterFilePath); ImageAndQRcode.writeToFile(bitMatrix, format, outputFile); ImageAndQRcode newImageUtils = new ImageAndQRcode(); BufferedImage buffImg = ImageAndQRcode.watermark(new File(sourceFilePath), new File(waterFilePath), maginx,
maginy, 1.0f); newImageUtils.generateWaterFile(buffImg, saveFilePath); ByteArrayOutputStream os = new ByteArrayOutputStream();
boolean flag = ImageIO.write(buffImg, "jpg", os);
byte[] byteArray = os.toByteArray();
return byteArray; } public static void main(String[] args) throws IOException { ImageAndQRcode add = new ImageAndQRcode(); /**
     * 
     * @param text 二维码内容
     * @param width 二维码图片宽度
     * @param height 二维码图片高度
     * @param format 二维码的图片格式
     * @param sourceFilePath 底层图片路径
     * @param waterFilePath 二维码路径
     * @param saveFilePath 合成图片路径
     * @param maginx  二维码距离底图x轴距离
     * @param maginy  二维码距离底图y轴距离
     * @throws Exception
     */
try { add.addImageQRcode("http://www.baidu.com", 300, 300, "jpg",
"C:/Users/admin/Desktop/1.png",
"C:/Users/admin/Desktop/new.jpg",
"C:/Users/admin/Desktop/3.png",
250, 250); } catch (Exception e) { e.printStackTrace(); }
}
}

调用案例

    public static byte[] t4(HttpServletRequest request,String t,String id){
String realPath = "";
if("1".equals(t)){
realPath = request.getSession().getServletContext().getRealPath("/static/images/1.png").replaceAll("\\\\", "/");
}else{
realPath= request.getSession().getServletContext().getRealPath("/static/images/2.png").replaceAll("\\\\", "/");
}
String uuid1 = UuidUtils.getUUID();
String uuid3 = UuidUtils.getUUID();
String realPath3= request.getSession().getServletContext().getRealPath("/static/images/)"+uuid1+"(.jpg").replaceAll("\\\\", "/");
String realPath4 = request.getSession().getServletContext().getRealPath("/static/images/)"+uuid3+"(.jpg").replaceAll("\\\\", "/");
String contentz = "www.skjiadao.com?id="+id+"&type="+t; byte[] addImageQRcode1;
try {
addImageQRcode1 = ImageAndQRcode.addImageQRcode(contentz, 300, 300, "jpg",
realPath,
realPath4,
realPath3,
250,
250);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new byte[1];
}
return addImageQRcode1; }

java生成二维码并融合模板工具类的更多相关文章

  1. java生成二维码的三个工具

    1.  使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode ...

  2. 利用JAVA生成二维码

    本文章整理于慕课网的学习视频<JAVA生成二维码>,如果想看视频内容请移步慕课网. 维基百科上对于二维码的解释. 二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图 ...

  3. java 生成二维码、可带LOGO、可去白边

      1.准备工作 所需jar包: JDK 1.6: commons-codec-1.11.jar core-2.2.jar javase-2.2.jar JDK 1.7: commons-codec- ...

  4. java 生成二维码后叠加LOGO并转换成base64

      1.代码 见文末推荐 2.测试 测试1:生成base64码 public static void main(String[] args) throws Exception { String dat ...

  5. java生成二维码打印到浏览器

    java生成二维码打印到浏览器   解决方法: pom.xml的依赖两个jar包: <!-- https://mvnrepository.com/artifact/com.google.zxin ...

  6. 二维码相关---java生成二维码名片,而且自己主动保存到手机通讯录中...

    版权声明:本文为博主原创文章,未经博主credreamer 同意不得转载 违者追究法律责任. https://blog.csdn.net/lidew521/article/details/244418 ...

  7. java生成二维码(需导入第三方ZXing.jar包)

    //这个类是用来解析,通过图片解析该图片的网页链接是什么 package util; import java.awt.Graphics2D;import java.awt.geom.AffineTra ...

  8. JAVA生成二维码图片代码

    首先需要导入 QRCode.jar 包 下载地址看这里   http://pan.baidu.com/s/1o6qRFqM import java.awt.Color;import java.awt. ...

  9. java生成二维码图片

    1.POM文件引入 <dependency> <groupId>com.google.zxing</groupId> <artifactId>core& ...

随机推荐

  1. 【原创】大数据基础之Kerberos(2)hive impala hdfs访问

    1 hive # kadmin.local -q 'ktadd -k /tmp/hive3.keytab -norandkey hive/server03@TEST.COM'# kinit -kt / ...

  2. 如何创建一个标准的Windows服务

    出处:http://www.cnblogs.com/wuhuacong/archive/2009/02/11/1381428.html 如何创建一个标准的Windows服务 在很多时候,我们需要一个定 ...

  3. OCP 相关课程列表

    OCP 相关课程列表 第一天:Linux基础 和 Oracle 11 R2 数据库安装教程图解 1:< VM 安装 linux Enterprise_R5_U4_Server_I386_DVD教 ...

  4. 快速理解VirtualBox的四种网络连接方式

    VirtualBox中有4中网络连接方式: NAT Bridged Adapter Internal Host-only Adapter VMWare中有三种,其实他跟VMWare 的网络连接方式都是 ...

  5. Confluence 6 使用 Apache 和 mod_proxy 添加 SSL和其他

    添加 SSL 如果你计划在你的应用中启用 SSL ,请参考 Securing your Atlassian applications with Apache using SSL页面中的内容,并确定你在 ...

  6. Confluence 6 让 Jira 应用停止发送通知到 Confluence

    你可能希望仅仅配置 Confluence 在 workbox 中仅仅显示自己的通知,禁用在 workbox 中显示从 Jira 来的通知.有可能这个 Jira 的应用已经通过应用链接功能正确链接到 C ...

  7. Confluence 6 管理插件和组件

    一个 组件(add-on)是与 Confluence 分开安装的功能,能够加强 Confluence 的功能和使用.插件(plugin)和 组件(add-on)这 2 个词通常是一起使用的. 一共有 ...

  8. django----注意事项

    不用带参数 必须要带参数:

  9. 在线版区间众数 hzw的代码。。

    /* 查询区间众数,要求强制在线 设有T个块 1.众数只可能在大块[L,R]里或者两端[l,L) (R,r]里出现 2.大块的众数只要预处理打表一下即可,复杂度n*T(这样的区间有T*T个) 3.两端 ...

  10. @ResponseBody//该注解会将返回值转为json格式并放到响应体中返回到前台