生成二维码跳转页面(转)

用的google二维码包写的
原文链接:https://www.iteye.com/blog/suko-2244138

原文没有配maven,这里给一份

     <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.0.0</version></dependency>

代码

package test;  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.OutputStream;
import java.util.Hashtable;
import java.util.Random;  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.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  public class QRCodeUtil {  private static final String CHARSET = "utf-8";    private static final String FORMAT_NAME = "JPG";    // 二维码尺寸    private static final int QRCODE_SIZE = 300;  // LOGO宽度    private static final int WIDTH = 60;  // LOGO高度    private static final int HEIGHT = 60;  private static BufferedImage createImage(String content, String imgPath,    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);    hints.put(EncodeHintType.MARGIN, 1);    BitMatrix bitMatrix = new MultiFormatWriter().encode(content,    BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_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;    }    // 插入图片    QRCodeUtil.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()) {    System.err.println(""+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();    }    /**  * 生成二维码(内嵌LOGO)  *   * @param content  *            内容  * @param imgPath  *            LOGO地址  * @param destPath  *            存放目录  * @param needCompress  *            是否压缩LOGO  * @throws Exception  */    public static void encode(String content, String imgPath, String destPath,    boolean needCompress) throws Exception {    BufferedImage image = QRCodeUtil.createImage(content, imgPath,    needCompress);    mkdirs(destPath);    String file = new Random().nextInt(99999999)+".jpg";    ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));    }    /**  * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)  * @author lanyuan  * Email: mmm333zzz520@163.com  * @date 2013-12-11 上午10:16:36  * @param destPath 存放目录  */    public static void mkdirs(String destPath) {    File file =new File(destPath);        //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)    if (!file.exists() && !file.isDirectory()) {    file.mkdirs();    }    }    /**  * 生成二维码(内嵌LOGO)  *   * @param content  *            内容  * @param imgPath  *            LOGO地址  * @param destPath  *            存储地址  * @throws Exception  */    public static void encode(String content, String imgPath, String destPath)    throws Exception {    QRCodeUtil.encode(content, imgPath, destPath, false);    }    /**  * 生成二维码  *   * @param content  *            内容  * @param destPath  *            存储地址  * @param needCompress  *            是否压缩LOGO  * @throws Exception  */    public static void encode(String content, String destPath,    boolean needCompress) throws Exception {    QRCodeUtil.encode(content, null, destPath, needCompress);    }    /**  * 生成二维码  *   * @param content  *            内容  * @param destPath  *            存储地址  * @throws Exception  */    public static void encode(String content, String destPath) throws Exception {    QRCodeUtil.encode(content, null, destPath, false);    }    /**  * 生成二维码(内嵌LOGO)  *   * @param content  *            内容  * @param imgPath  *            LOGO地址  * @param output  *            输出流  * @param needCompress  *            是否压缩LOGO  * @throws Exception  */    public static void encode(String content, String imgPath,    OutputStream output, boolean needCompress) throws Exception {    BufferedImage image = QRCodeUtil.createImage(content, imgPath,    needCompress);    ImageIO.write(image, FORMAT_NAME, output);    }    /**  * 生成二维码  *   * @param content  *            内容  * @param output  *            输出流  * @throws Exception  */    public static void encode(String content, OutputStream output)    throws Exception {    QRCodeUtil.encode(content, null, output, false);    }    /**  * 解析二维码  *   * @param file  *            二维码图片  * @return  * @throws Exception  */    public static String decode(File file) throws Exception {    BufferedImage image;    image = ImageIO.read(file);    if (image == null) {    return null;    }    BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(    image);    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));    Result result;    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();    hints.put(DecodeHintType.CHARACTER_SET, CHARSET);    result = new MultiFormatReader().decode(bitmap, hints);    String resultStr = result.getText();    return resultStr;    }    /**  * 解析二维码  *   * @param path  *            二维码图片地址  * @return  * @throws Exception  */    public static String decode(String path) throws Exception {    return QRCodeUtil.decode(new File(path));    }  public static void main(String[] args) throws Exception {  String text = "http://www.iteye.com";  //这里设置自定义网站url  String logoPath = "d:\\zilv.jpg";   String destPath = "d:\\";   QRCodeUtil.encode(text, logoPath, destPath, true);    }
}  

生成二维码跳转页面(转)相关推荐

  1. tp框架 后台 利用二维码生成器 生成二维码 跳转页面

    1.先在某个控制器里写一个拼接跳转页面链接信息的方法,如: 要跳转的链接是 http://xxxxx.xxx.cn/index.php?s=/Admin/Test/index/id/101/cid/1 ...

  2. 微信扫描二维码跳转页面

    微信扫描二维码跳转页面 近在完成一个大作业,反正一个小部分就是扫描二维码,跳转到一个界面去,搜网上也没有什么太有用的信息,觉得难死了.. 后来想想,以前写过一个程序,就是把字符串生成相应的二维码,然后 ...

  3. Vue+element-ui上传logo图片到后端生成二维码展示到页面

    Vue+element-ui上传logo图片生成二维码展示到页面 该文章将介绍如何通过前端上传二维码logo图片在后台生成二维码,并将生成的二维码转换成Base64编码返回给前端在页面展示,用户扫码二 ...

  4. vue H5页面跳转微信小程序以及生成二维码跳转小程序

    研究了两种H5跳转小程序的方法,同时携带参数 1. 手机浏览器打开H5页面,点击按钮拉起微信小程序 2. H5页面上生成小程序二维码,手机微信扫码跳转目标小程序 为了开发方便,以上两种均借助了微信小程 ...

  5. 将链接转成base64格式生成二维码和把页面生成图片

    将链接转成base64格式 function getUrlBase64 (url, etx, callback) {var canvas = document.createElement('canva ...

  6. JS生成二维码以及保存页面为图片的解决方案:html2Canvas+file-saver+qrcodejs2的使用心得以及解决图片失真,保存不完整的解决办法

    前言 最近因为工作需求,需要前端根据后端传过来的链接生成二维码,并且要使用JS保存页面为图片.然后网上搜了很多解决办法.最终都是用h5的canvas进行绘制然后保存为图片.其中,又以html2Canv ...

  7. 纯前端实现一键生成二维码,打开新页面展示二维码

    如何实现这个需求呢首先我们需要生成二维码,而且要打开一个新的页面展示,那么我们需要img标签来展示图片的载体,那么生成图片src必不可少的.无论我们的项目是spa,还是多页面应用,我们这里都要用bas ...

  8. 前端生成二维码及把页面转为图片保存到本地

    以vue项目为例,其他类型项目其实也是一样的 所需components: QRCode html2canvas npm install qrcodejs2 -- save npm install ht ...

  9. 怎样微信扫描二维码跳转页面,H5页面在微信中下载APP的实现方式

    使用微信推广的用户经常都会遇到推广链接被拦截导致无法下载app的情况,此时用户在微信中打开会提示" 已停止访问该网页 ".这对于使用微信营销的商家来说就很不友好且损失非常大,因为用 ...

最新文章

  1. Spark配置属性详解
  2. 【CF1020C】Election【贪心】
  3. iOS设置拍照retake和use按钮为中文简体
  4. htc816t Android go,HTC D816t(Desire D816t 移动4G)一键刷机图文教程详解
  5. mysql 编码种类_MySQL 编码
  6. 一个比CAM350好用的看GERBER软件
  7. 【C++学习(一)】iostream和iostream.h和stdio.h的区别
  8. realtek 8111E 网卡 修改MAC 地址
  9. 电子计算机的两个重要改进是,1946年,冯.诺依曼在研制EDVAC计算机时,提出的两个重要改进是什么?...
  10. SAP SLT数据同步配置
  11. 手机点餐系统概述_廖师兄 微信点餐系统 springcloud学习笔记
  12. pybind11中文资料(第五章 面向对象的代码)
  13. 警惕非上架应用的下载和使用
  14. freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
  15. GitHub里的MySQL基础架构自动化测试
  16. Android获取OAID
  17. Linux 压缩文件和文件打包。
  18. mysql报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zo
  19. 算法C++ DepthFirstSearch BreadthFirstSearch代码模式示范实现(第四章)
  20. 各大网站 http server分析

热门文章

  1. C#超简单实现资产折旧计算方法
  2. explorer.exe
  3. ecshop导入淘宝助理导出的数据(出现乱码)
  4. 数字标牌无线联网方案
  5. 内点法最优潮流matlab程序
  6. .chm文件打不开或者不显示具体内容
  7. 土木向计算机转专业,【土木转行·续集】土建类同学转行计算机,什么时间转最合适?...
  8. markdown编辑器调整图片大小
  9. hdu 小t的游戏(找规律)
  10. uni-app隐藏顶部导航栏