|
@@ -5,7 +5,9 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.BarcodeFormat;
|
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.EncodeHintType;
|
|
|
import com.google.zxing.WriterException;
|
|
import com.google.zxing.WriterException;
|
|
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
|
+import com.google.zxing.oned.Code128Writer;
|
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -34,7 +36,7 @@ public class CodeUtils {
|
|
|
* @param logoUrl logo图片路径
|
|
* @param logoUrl logo图片路径
|
|
|
* @param content 二维码内容
|
|
* @param content 二维码内容
|
|
|
* */
|
|
* */
|
|
|
- public static String createCode(String logoUrl,Object content){
|
|
|
|
|
|
|
+ public static String createCode(String logoUrl,String content){
|
|
|
// 二维码大小
|
|
// 二维码大小
|
|
|
int width = 500, height = 500;
|
|
int width = 500, height = 500;
|
|
|
// 二维码参数
|
|
// 二维码参数
|
|
@@ -55,7 +57,7 @@ public class CodeUtils {
|
|
|
BitMatrix bm = null;
|
|
BitMatrix bm = null;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- bm = writer.encode(JSON.toJSONString(content), BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);
|
|
|
|
|
|
|
+ bm = writer.encode(content, BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);
|
|
|
} catch (WriterException e) {
|
|
} catch (WriterException e) {
|
|
|
log.error("二维码生成异常:"+e.getMessage());
|
|
log.error("二维码生成异常:"+e.getMessage());
|
|
|
}
|
|
}
|
|
@@ -118,20 +120,7 @@ public class CodeUtils {
|
|
|
qrcodeBuffImg.flush();
|
|
qrcodeBuffImg.flush();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
|
|
|
|
|
- try {
|
|
|
|
|
- ImageIO.write(qrcodeBuffImg, "png", baos);//写入流中
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("二维码生成异常:"+e.getMessage());
|
|
|
|
|
- }
|
|
|
|
|
- byte[] bytes = baos.toByteArray();//转换成字节
|
|
|
|
|
- BASE64Encoder encoder = new BASE64Encoder();
|
|
|
|
|
- String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
|
|
|
|
|
- png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- return PRE_FIX+png_base64;
|
|
|
|
|
|
|
+ return toBase64(qrcodeBuffImg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -139,59 +128,13 @@ public class CodeUtils {
|
|
|
* 生成条形码
|
|
* 生成条形码
|
|
|
* @param content 条形码内容
|
|
* @param content 条形码内容
|
|
|
* */
|
|
* */
|
|
|
- public static String createBar(Object content){
|
|
|
|
|
- // 二维码大小
|
|
|
|
|
- int width = 500, height = 500;
|
|
|
|
|
- // 二维码参数
|
|
|
|
|
- CodeStyle style = new CodeStyle();
|
|
|
|
|
- style.setWidth(width);
|
|
|
|
|
- style.setHeight(height);
|
|
|
|
|
-
|
|
|
|
|
- Map<EncodeHintType, Object> hints = new HashMap<>();
|
|
|
|
|
- //内容编码格式
|
|
|
|
|
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
|
|
|
- // 指定纠错等级
|
|
|
|
|
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
|
|
|
|
|
- //设置二维码边的空度,非负数
|
|
|
|
|
- hints.put(EncodeHintType.MARGIN, 1);
|
|
|
|
|
-
|
|
|
|
|
- // 生成二维码图片
|
|
|
|
|
- QRCodeWriter writer = new QRCodeWriter();
|
|
|
|
|
- BitMatrix bm = null;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- bm = writer.encode(JSON.toJSONString(content), BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);
|
|
|
|
|
- } catch (WriterException e) {
|
|
|
|
|
- log.error("二维码生成异常:"+e.getMessage());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int margin = style.getMargin();
|
|
|
|
|
- int tempM = margin*2;
|
|
|
|
|
- int[] rec = bm.getEnclosingRectangle(); //获取二维码图案的属性
|
|
|
|
|
- int resWidth = rec[2] + tempM;
|
|
|
|
|
- int resHeight = rec[3] + tempM;
|
|
|
|
|
- BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix
|
|
|
|
|
- resMatrix.clear();
|
|
|
|
|
- for (int i = margin; i < resWidth - margin; i++) { //循环,将二维码图案绘制到新的bitMatrix中
|
|
|
|
|
- for (int j = margin; j < resHeight - margin; j++){
|
|
|
|
|
- if (bm.get(i - margin + rec[0], j - margin + rec[1])){
|
|
|
|
|
- resMatrix.set(i, j);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- bm = resMatrix;
|
|
|
|
|
-
|
|
|
|
|
- int w = bm.getWidth();
|
|
|
|
|
- int h = bm.getHeight();
|
|
|
|
|
- BufferedImage qrcodeBuffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
-
|
|
|
|
|
- // 开始利用二维码数据创建Bitmap图片
|
|
|
|
|
- for (int x = 0; x < w; x++) {
|
|
|
|
|
- for (int y = 0; y < h; y++) {
|
|
|
|
|
- qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? style.getCodeColor() : style.getBackgroundColor());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public static String createBar(String content){
|
|
|
|
|
+ BitMatrix bitMatrix = new Code128Writer().encode(content, BarcodeFormat.CODE_128, 300, 80, null);
|
|
|
|
|
+ BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
|
|
|
|
|
+ return toBase64(bufferedImage);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ private static String toBase64(BufferedImage qrcodeBuffImg){
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
|
|
|
try {
|
|
try {
|
|
|
ImageIO.write(qrcodeBuffImg, "png", baos);//写入流中
|
|
ImageIO.write(qrcodeBuffImg, "png", baos);//写入流中
|
|
@@ -201,9 +144,7 @@ public class CodeUtils {
|
|
|
byte[] bytes = baos.toByteArray();//转换成字节
|
|
byte[] bytes = baos.toByteArray();//转换成字节
|
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
|
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
|
|
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
|
|
|
- png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+ png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");
|
|
|
return PRE_FIX+png_base64;
|
|
return PRE_FIX+png_base64;
|
|
|
}
|
|
}
|
|
|
|
|
|