二维码融合模板

二维码融合图片

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. spark-streaming集成Kafka处理实时数据

    在这篇文章里,我们模拟了一个场景,实时分析订单数据,统计实时收益. 场景模拟 我试图覆盖工程上最为常用的一个场景: 1)首先,向Kafka里实时的写入订单数据,JSON格式,包含订单ID-订单类型-订 ...

  2. IPNS节点ID

    IPNS节点ID访问网站:     当我们修改网站内容重新添加到ipfs时,hash会发生变化,当我们网站更新时,我们可以将网站发布到IPNS,在IPNS中,允许我们节点的域名空间中引用一个IPFS ...

  3. Codeforces 833D Red-Black Cobweb [点分治]

    洛谷 Codeforces 思路 看到树上路径的统计,容易想到点分治. 虽然只有一个限制,但这个限制比较麻烦,我们把它拆成两个. 设黑边有\(a\)条,白边有\(b\)条,那么有 \[ 2a\geq ...

  4. kindeditor用法简单介绍(转)

    1,首先去官网下载http://www.kindsoft.net/ 2,解压之后如图所示: 由于本人做的是用的是JSP,所以ASP,PHP什么的就用不上了,直接把那些去掉然后将整个文件夹扔进Myecl ...

  5. C# 通用数据库配置界面,微软原生DLL重整合

    C# 通用数据库配置界面,微软原生DLL重整合 自己整合了 微软自带的数据连接配置界面对话库    Microsoft.Data.ConnectionUI.Dialog.dll  微软自带的数据连接配 ...

  6. kindEditor 富文本编辑器 使用介绍

    第一版:存放位置:  ---->把该创建的文件包放到javaWeb 过程的 WEB_INF 下:如图所示. 第二步:< kindEditor 插件的引用> :JS引用 <scr ...

  7. Confluence 6 启用主题评论

    页面或者博客页面中显示的评论以下面 2 种方式显示: 主题模式(Threaded):以继承回复的方式显示页面的评论.每一回复的评论将会在不同评论之间显示,以表示各个评论之间的关系. 平面模式(Flat ...

  8. Confluence 6 查看系统信息

    系统信息界面提供了有关 Confluence 的配置信息和 Confluence 部署的环境信息. 希望对你的系统信息进行查看: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Confi ...

  9. web的分页方法

    web分页的三种方式,闲来无事总结一下. 1.使用前端表格插件进行分页 例如用bootstrap的拓展table组件,注意设置其分页属性时设置为"client", 即是 sideP ...

  10. Java之递归方法的字符串回文问题

    日期:2018.10.12 星期五 博客期:018 题目: 题目分析:本题目因为是要求用递归的,所以大类里就写一个递归方法,在主方法里用字符串调用这个方法就好了!这是大致这个类的框架定位,然后定位我们 ...