자바 로그 가 있 는 QR 코드 생 성
11259 단어 QR 코드 생 성
com.google.zxing
javase
3.1.0
com.google.zxing
core
3.3.3
compile
package com.centnet.chakra.core.utils;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.*;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.*;
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;
/**
*
*
* @author 。。。。
*/
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
private static final String FORMAT = "JPG";
/**
*
*/
private static final int QRCODE_SIZE = 300;
/**
* LOGO
*/
private static final int LOGO_WIDTH = 60;
/**
* LOGO
*/
private static final int LOGO_HEIGHT = 60;
/**
* LOGO
*
* @param source
* @param logoPath LOGO
* @param needCompress
* @throws Exception
*/
private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception {
File file = new File(logoPath);
if (!file.exists()) {
throw new Exception("logo file not found.");
}
Image src = ImageIO.read(new File(logoPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
// LOGO
if (needCompress) {
if (width > LOGO_WIDTH) {
width = LOGO_WIDTH;
}
if (height > LOGO_HEIGHT) {
height = LOGO_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();
}
private static BufferedImage createImage(String content, String logoPath, 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 (logoPath == null || "".equals(logoPath)) {
return image;
}
//
QRCodeUtil.insertImage(image, logoPath, needCompress);
return image;
}
/**
* ( LOGO)
* ,
*
* @param content
* @param logoPath LOGO
* @param destPath
* @param needCompress LOGO
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
mkdirs(destPath);
String fileName = new Random().nextInt(99999999) + "." + FORMAT.toLowerCase();
ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
return fileName;
}
/**
* ( LOGO)
*
*
* @param content
* @param logoPath LOGO
* @param destPath
* @param fileName
* @param needCompress LOGO
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath, String fileName, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
mkdirs(destPath);
fileName = fileName.substring(0, fileName.indexOf(".") > 0 ? fileName.indexOf(".") : fileName.length())
+ "." + FORMAT.toLowerCase();
ImageIO.write(image, FORMAT, new File(destPath + "/" + fileName));
return fileName;
}
/**
* ,mkdirs , mkdir.
* (mkdir )
*
* @param destPath
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* ( LOGO)
*
* @param content
* @param logoPath LOGO
* @param destPath
* @throws Exception
*/
public static String encode(String content, String logoPath, String destPath) throws Exception {
return QRCodeUtil.encode(content, logoPath, destPath, false);
}
/**
*
*
* @param content
* @param destPath
* @param needCompress LOGO
* @throws Exception
*/
public static String encode(String content, String destPath, boolean needCompress) throws Exception {
return QRCodeUtil.encode(content, null, destPath, needCompress);
}
/**
*
*
* @param content
* @param destPath
* @throws Exception
*/
public static String encode(String content, String destPath) throws Exception {
return QRCodeUtil.encode(content, null, destPath, false);
}
/**
* ( LOGO)
*
* @param content
* @param logoPath LOGO
* @param output
* @param needCompress LOGO
* @throws Exception
*/
public static void encode(String content, String logoPath, OutputStream output, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
ImageIO.write(image, FORMAT, output);
}
/**
*
*
* @param content
* @param output
* @throws Exception
*/
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
/**
*
* @param content
* @param logoPath logo
* @param destPath
* @param fileName
* @param effectiveTimes ( , )
* @param effectiveSecond ( )
*/
public static void encode(String content,String logoPath,String destPath,String fileName ,Integer effectiveTimes,Integer effectiveSecond) throws Exception{
//
if(null == effectiveTimes){
// :100
effectiveTimes = 100 ;
}
if(null == effectiveSecond){
// :24H
effectiveSecond = 24*60*60 ;
}
String redisKey = "QR"+NumberUtil.genOutCaseNum() ;
RedisUtil.hset(redisKey,redisKey,effectiveTimes.toString(),effectiveSecond);
//todo http://forzh.xiaomy.net
//
String qrUrl = "http://forzh.xiaomy.net"+"/qrCode/checkQRCode?"+"key="+redisKey+"&content="+content;
//
encode(qrUrl, logoPath, destPath, fileName, true);
}
/**
*
* @param content
* @param effectiveTimes ( , )
* @param effectiveSecond ( )
*/
public static byte[] encode(String content,Integer effectiveTimes,Integer effectiveSecond){
//
if(null == effectiveTimes){
// :100
effectiveTimes = 100 ;
}
if(null == effectiveSecond){
// :24H
effectiveSecond = 24*60*60 ;
}
String redisKey = "QR"+NumberUtil.genOutCaseNum() ;
RedisUtil.hset(redisKey,redisKey,effectiveTimes.toString(),effectiveSecond);
//todo http://forzh.xiaomy.net
//
String qrUrl = "http://forzh.xiaomy.net/qrCode/checkQRCode?"+"key="+redisKey+"&content="+content;
return QrCodeUtil.generatePng(qrUrl, new QrConfig());
}
/**
*
*
* @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));
}
public static void main(String[] args) throws Exception {
String text = "https://www.baidu.com";
// Logo,
QRCodeUtil.encode(text, "C:\\Users\\gc\\Desktop\\ .jpg", "e:\\", " ", null,null);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
php 판 로고 가 있 는 QR 코드 소스 코드 생 성텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.