update
This commit is contained in:
@@ -5,23 +5,24 @@ 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;
|
||||
import org.apache.tomcat.util.codec.binary.Base64;
|
||||
import com.lovenav.dao.CollectIconListDao;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.RoundRectangle2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Base64;
|
||||
|
||||
|
||||
@Component
|
||||
@Service
|
||||
public class QRCodeUtil {
|
||||
private static final String CHARSET = "utf-8";
|
||||
private static final String FORMAT_NAME = "JPG";
|
||||
@@ -313,24 +314,14 @@ public class QRCodeUtil {
|
||||
return image;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String text = "https://www.baidu.com"; //这里设置自定义网站url
|
||||
String logoPath ="src/main/resources/static/logo/NAV.png";
|
||||
String destPath = "src/main/resources/static/qr";
|
||||
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);*/
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片url转换成base64流
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public String getBase64(String url){
|
||||
public static String getBase64(String url){
|
||||
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
|
||||
Map hints = new HashMap();
|
||||
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
|
||||
@@ -343,8 +334,8 @@ public class QRCodeUtil {
|
||||
//输出二维码图片流
|
||||
try {
|
||||
ImageIO.write(image, "jpg",outputStream);
|
||||
String base = new Base64().encodeAsString(outputStream.toByteArray());
|
||||
// new Base64().encode(outputStream.toByteArray());
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
String base =encoder.encodeToString(outputStream.toByteArray());
|
||||
return base;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
@@ -371,4 +362,38 @@ public class QRCodeUtil {
|
||||
return image;
|
||||
}
|
||||
|
||||
// 获取本地图片路径,转为base64
|
||||
public static String ImageToBase64(String imgPath) {
|
||||
byte[] data = null;
|
||||
// 读取图片字节数组
|
||||
try {
|
||||
InputStream in = new FileInputStream(imgPath);
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
// 返回Base64编码过的字节数组字符串
|
||||
// System.out.println("本地图片转换Base64:" + encoder.encodeToString(data));
|
||||
return encoder.encodeToString(data);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String text = "https://www.baidu.com"; //这里设置自定义网站url
|
||||
String logoPath ="src/main/resources/static/logo/NAV.png";
|
||||
String destPath = "src/main/resources/static/qr";
|
||||
String url = QRCodeUtil.encode(text, logoPath, destPath, true);
|
||||
QRCodeUtil.ImageToBase64("src/main/resources/static/qr/44672405.jpg");
|
||||
// System.out.println(url);
|
||||
// String base64 = getBase64(url);
|
||||
// System.out.println(base64);
|
||||
|
||||
/* String file="D:\\11.jpg";
|
||||
String info=QRCodeUtil.decode(file);
|
||||
System.out.println(info);*/
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user