간단하게 QR 코드 생 성
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.alibaba.fastjson.JSONObject;
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.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;
/**
* Created by robin on 2018/6/4.
*/
public class QRCodeUtil {
private final static String FILE_URL_YOUR_CONFIG = "C:\\Users\\29007\\Desktop\\";//
private final static String IMAGE_FORMAT = ".png";//
private final static Integer IMAGE_SIZE = 900;// 900 700*700 8k > 200
/**
*
* @param fileName
* @param qrCode
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(String fileName, QRCode qrCode) throws WriterException, IOException{
return createQrCode(FILE_URL_YOUR_CONFIG,fileName,qrCode,IMAGE_SIZE,IMAGE_FORMAT);
}
/**
*
* @param outputUrl
* @param fileName
* @param qrCode
* @param qrCodeSize
* @param imageFormat
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(String outputUrl,String fileName, QRCode qrCode, int qrCodeSize, String imageFormat) throws WriterException, IOException{
String url = outputUrl + fileName + imageFormat;
FileOutputStream outputStream = new FileOutputStream(new File(url));
String content = JSONObject.toJSONString(qrCode);
return createQrCode(outputStream,content,qrCodeSize,imageFormat);
}
/**
*
* @param outputStream
* @param content
* @param qrCodeSize
* @param imageFormat
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
//
content = new String(content.getBytes("UTF-8"),"ISO-8859-1");
// MAP
Hashtable, ErrorCorrectionLevel> hintMap = new Hashtable, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// ( ) QR
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// BufferedImage QRCode (matrixWidth )
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
//
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-100, j-100, 1, 1);
}
}
}
return ImageIO.write(image,imageFormat.substring(1,imageFormat.length()), outputStream);
}
/**
*
*/
public static QRCode readQrCode(String fileName) throws IOException, FormatException, ChecksumException, NotFoundException {
String inputUrl = FILE_URL_YOUR_CONFIG + fileName + IMAGE_FORMAT;
FileInputStream inputStream = new FileInputStream(new File(inputUrl));
return readQrCode(inputStream);
}
/**
*
*/
public static QRCode readQrCode(String inputUrl,String fileName,String imageFormat) throws IOException, FormatException, ChecksumException, NotFoundException {
String url = inputUrl + fileName + imageFormat;
FileInputStream inputStream = new FileInputStream(new File(url));
return readQrCode(inputStream);
}
/**
*
*/
public static QRCode readQrCode(InputStream inputStream) throws IOException, FormatException, ChecksumException, NotFoundException {
//
BufferedImage image = ImageIO.read(inputStream);
//
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
QRCode qrCode = new QRCode();
Result result = reader.decode(bitmap);
String text = result.getText();
if(StringUtils.isNotBlank(text)){
qrCode = JSONObject.parseObject(text, QRCode.class);
}
return qrCode;
}
/**
*
* @throws WriterException
*/
public static void main(String[] args) throws IOException, WriterException {
try {
QRCode qrCode = new QRCode();
qrCode.setPhone("17712340000");
String fileName = "3";
/* */
createQrCode(fileName,qrCode);
/* */
readQrCode(fileName);
} catch (FormatException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
/**
* bean
*/
public static class QRCode {
private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
}
:https://blog.csdn.net/lixuegen/article/details/80886225
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
요구사항 정의요구사항 정의 작성 방법 개요 ・목적 표시되고 있는 텍스트를 가변으로 한다 · 과제 표시된 텍스트가 변경되지 않음 ・해결 표시되고 있는 텍스트가 가변이 된다 사양 · 표시 정의 각 편집 화면 ○○ 표시되고 있는 텍스...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.