java将一个url链接或者文字生成二维码并且转成base64

您所在的位置:网站首页 如何将二维码生成链接 java将一个url链接或者文字生成二维码并且转成base64

java将一个url链接或者文字生成二维码并且转成base64

2023-09-25 03:31| 来源: 网络整理| 查看: 265

我们在开发的时候,肯定有这样的需求。把一个url链接转成二维码图片。提供给用户扫描,然后跳转到相应的页面。

三个问题(前提:没有用统一的图片服务器如:fastdfs。)

(1):把url链接转成图片保存。这样只需要提供图片的地址就行。

(2):但是有的时候,需要提供base64图片流。这是因为,图片可能是在内网,不能通过ip转到外面。

(3):当使用分布式服务器的时候,生成的图片的地址不一样,如果查找的时候,就不利于管理。这个时候,可以用不需要生成图片的方式。

解决第一个问题:

(1):需要使用生成二维码的jar包。zxing是比较通用的jar包。

com.google.zxing core 3.3.0 com.google.zxing javase 3.3.0

(2):编写生成二维码的util.也就是Qrcode.util

package com.iflytek.util; 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.client.j2se.BufferedImageLuminanceSource; 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 hints = new Hashtable(); 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 String 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)); return destPath+"/"+file; } /** * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) * @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 hints = new Hashtable(); 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)); } /** * * @param content * @param imgPath * @param destPath * @param localUrl 当前环境的域名或者ip地址 * @param needCompress * @return * @throws Exception */ public static String encode(String content, String imgPath, String destPath,String localUrl, 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)); return localUrl+file; } public static String encodeDZ(String content, String imgPath, String destPath,String localUrl,int length,int width, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImageDZ(content, imgPath,length,width, needCompress); mkdirs(destPath); String file = new Random().nextInt(99999999)+".jpg"; ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file)); return localUrl+file; } private static BufferedImage createImageDZ(String content, String imgPath,int newlength,int newWidth, boolean needCompress) throws Exception { Hashtable hints = new Hashtable(); 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, newlength, newWidth, 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; } public static void main(String[] args) throws Exception { String text = "https://www.yoyow.org/files/d2b3b82351bda9e70b92c6ee219a432b.png"; //这里设置自定义网站url String logoPath =null; String destPath = "d:\\pic\\"; String url=QRCodeUtil.encode(text, logoPath, destPath, true); System.out.println(url); /* String file="D:\\11.jpg"; String info=QRCodeUtil.decode(file); System.out.println(info);*/ } }

使用QRCodeUtil.encode("url链接地址","logo的地址","生成的图片放在的位置(jpg图片的物理地址如:opt/项目名/jpg/)")。

(3):访问生成的图片,需要在一个config即MvcConfig

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.web.filter.CommonsRequestLoggingFilter; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { private static Logger logger= LoggerFactory.getLogger(MvcConfig.class); @Autowired private Environment environment; /** *图片等地址映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { logger.info("pdfURL="+environment.getProperty("qr.location.url")); registry.addResourceHandler("/pdf/**").addResourceLocations("file:"+environment.getProperty("qr.location.url")); registry.addResourceHandler("/excel/**").addResourceLocations("file:"+environment.getProperty("excel.location.url")); super.addResourceHandlers(registry); } @Bean public CommonsRequestLoggingFilter requestLoggingFilter() { CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter(); loggingFilter.setMaxPayloadLength(2048); loggingFilter.setIncludeClientInfo(true); loggingFilter.setIncludeQueryString(true); loggingFilter.setIncludePayload(true); loggingFilter.setIncludeHeaders(true); return loggingFilter; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }

(4):在配置文件application.properties

#生成二维码物理地址 qr.location.url=D:/mnt/hmzhao/analytics/pic/

这样生成的图片就可以通过

ip+port+项目名称+/pdf/+生成图片名称+.jpg

如:http://192.168.171.219:8088/test/pdf/12345.jpg

解决第二个问题

解决了第一个问题。第二个问题就稍微简单些,只需要把生成的图片转成base64流就可以。

(1):将图片流转换成base64流的util.PicBaseUtil.java

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; import sun.misc.BASE64Encoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; public class PicBaseUtil { /** * 将本地图片转换成Base64编码字符串 * * @param imgFile 图片目录路径 * @return */ public String getImgFileToBase64(String imgFile) { //将图片文件转化为字节数组字符串,并对其进行Base64编码处理 InputStream inputStream = null; byte[] buffer = null; //读取图片字节数组 try { inputStream = new FileInputStream(imgFile); int count = 0; while (count == 0) { count = inputStream.available(); } buffer = new byte[count]; inputStream.read(buffer); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { // 关闭inputStream流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 对字节数组Base64编码 return new BASE64Encoder().encode(buffer); } }

(2):由于第一个问题中生成的图片放在一个物理地址上,所以,第二个问题需要第一个问题的物理地址。

String localPic="图片的物理地址"; String base= (new PicBaseUtil()).getImgFileToBase64(localPic);

这样返回的base64就是提供给前端的base64流。

解决第三个问题

解决第三个问题,不需要使用第一,第二个问题的结果,直接调用方法就可以生成了。

(1)转换的代码块

/** * 将图片url转换成base64流 * @param url * @return */ public String getBase64(String url){ MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型 hints.put(EncodeHintType.MARGIN, 1); //设置白边 BitMatrix bitMatrix = null; try { bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300,hints); BufferedImage image = toBufferedImage(bitMatrix); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //输出二维码图片流 try { ImageIO.write(image, "jpg",outputStream); String base =new BASE64Encoder().encode(outputStream.toByteArray()); return base; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (WriterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; } public 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) ? 0xFF000000 : 0xFFFFFFFF);//0xFF000000黑;0xFFFFFFFF白 } } return image; }

(2):使用方法。直接将url链接转成base64流。

String url="url链接"; String base= (new PicBaseUtil()).getBase64(url);

这样返回的base64就是提供给前端的base64流。

参考:Java通过图片url地址获取图片base64位字符串的两种方式



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3