130 lines
3.3 KiB
Java
130 lines
3.3 KiB
Java
package com.mitac.cell.barcode;
|
|
|
|
import com.google.common.base.Strings;
|
|
import com.google.common.io.Files;
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.WriterException;
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.EnumMap;
|
|
import java.util.Map;
|
|
import javax.imageio.ImageIO;
|
|
|
|
public class QrCode {
|
|
private static final int DEFAULT_WIDTH = 220;
|
|
private static final int DEFAULT_MARGIN = 1;
|
|
private String code;
|
|
private int width = 220;
|
|
private int margin = 1;
|
|
|
|
public QrCode() {
|
|
}
|
|
|
|
public QrCode(String code, int width) {
|
|
this.setCode(code);
|
|
this.setWidth(width);
|
|
}
|
|
|
|
public String getCode() {
|
|
return this.code;
|
|
}
|
|
|
|
public void setCode(String code) {
|
|
this.code = code;
|
|
}
|
|
|
|
public int getWdith() {
|
|
return this.width;
|
|
}
|
|
|
|
public void setWidth(int width) {
|
|
this.width = width;
|
|
}
|
|
|
|
public int getMargin() {
|
|
return this.margin;
|
|
}
|
|
|
|
public void setMargin(int margin) {
|
|
this.margin = margin;
|
|
}
|
|
|
|
public BufferedImage toImage() {
|
|
return toImage(this.code, this.width, this.margin);
|
|
}
|
|
|
|
public byte[] toBytes(String fileFormat) {
|
|
return toBytes(this.toImage(), fileFormat);
|
|
}
|
|
|
|
public File saveAs(String filePath) {
|
|
if (Strings.isNullOrEmpty(filePath)) {
|
|
return null;
|
|
} else {
|
|
String ext = Files.getFileExtension(filePath);
|
|
return this.saveAs(filePath, ext);
|
|
}
|
|
}
|
|
|
|
public File saveAs(String filePath, String fileFormat) {
|
|
if (Strings.isNullOrEmpty(fileFormat)) {
|
|
fileFormat = "png";
|
|
}
|
|
|
|
try {
|
|
File outputFile = new File(filePath);
|
|
ImageIO.write(this.toImage(), fileFormat, outputFile);
|
|
return outputFile;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static byte[] toBytes(BufferedImage image, String fileFormat) {
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
try {
|
|
ImageIO.write(image, fileFormat, baos);
|
|
return baos.toByteArray();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static BufferedImage toImage(String code, Integer size, Integer margin) {
|
|
if (Strings.isNullOrEmpty(code)) {
|
|
return null;
|
|
} else {
|
|
if (size == null || size <= 16) {
|
|
size = 220;
|
|
}
|
|
|
|
if (margin == null || margin < 0) {
|
|
margin = 1;
|
|
}
|
|
|
|
Map<EncodeHintType, Object> hints = new EnumMap(EncodeHintType.class);
|
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
hints.put(EncodeHintType.MARGIN, margin);
|
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
|
|
|
|
try {
|
|
BitMatrix bitMatrix = (new QRCodeWriter()).encode(code, BarcodeFormat.QR_CODE, size, size, hints);
|
|
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
|
} catch (WriterException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|