二维码生成 条形码生成 windows可用
package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Hashtable; import java.util.List; import java.util.Locale; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; 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.pdf417.PDF417Writer; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class texts { private static final String KEY = "uuid"; private static final String SIZE = "msize"; private static final String IMAGETYPE = "png"; private static final String ZIMA = "zima"; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; public static void main(String[] args) throws IOException { String zima= ZIMA; String uuid = KEY ; // 获取uuid,组装公文信息 if (uuid != null && !"".equals(uuid)) { ServletOutputStream stream = null; try { // SwdjForm swform = QueryCache.get(SwdjForm.class, uuid); String swform="11"; //TwoBarcode barcode = new TwoBarcode().getBarcode(); String result = ""; if (swform != null){ /* if(StringHelper.isNotEmpty(zima)&&zima!=null&&!("null").equals(zima)){ swform.setBarCode(zima); }*/ //result = barcode.toString2(swform); result = toString2(swform); } System.out.println("-----result:"+result); //stream = resp.getOutputStream(); Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //PDF417Writer pdf417Writer = new PDF417Writer(); // 长二维码 BitMatrix m = getBCode(result, 504, 156,hints); System.out.println("-------m."+m.getWidth()); // 正方形二维码 //BitMatrix m = getQCode(result,size); BitMatrix ms = getQCode(result, 504); File outputFile = new File("E:" + File.separator + "1qqq23.jpg"); MatrixToImageWriter.writeToFile(ms, IMAGETYPE, outputFile); } catch (WriterException e) { e.printStackTrace(); } finally { if (stream != null) { stream.flush(); stream.close(); } } } } private static BitMatrix getQCode(String content, int size) throws UnsupportedEncodingException, WriterException { QRCodeWriter writer = new QRCodeWriter(); return writer.encode(URLEncoder.encode(content, "UTF-8"), BarcodeFormat.QR_CODE, size, size); } public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } //收文打印条码 public static String toString2(String swform) { /* if (swform != null) { swformToString(swform); }*/ StringBuffer result = new StringBuffer(); result.append("GB0626-2005" + "^"); result.append("123" + "^"); result.append("测试单位" + "^");//发文单位 result.append("测试种类" + "^");//公文种类或者刊物名称 result.append("001" + "^");//发文字号或者期号 result.append("" + "^");//.主送单位() /*if(title.length()>=22) { String title2 = title.substring(0,22)+"..."; //System.out.print(title2); result.append(title2 + "^"); }else{ result.append(title + "^");//标题 }*/ //result.append(title + "^");//标题 result.append("1"+ "^");//秘密等级 result.append("加急" + "^");//紧急程度 result.append("2020-11-16" + "^");//成文日期 result.append("" + "^");//发布层次() result.append("测试" + "^");//条码制作单位 result.append("" + "^");//条码制作日期 result.append("" + "^");//自定义字段(承办单位) result.append("|"); return result.toString(); } private static BitMatrix getBCode(String content, int width, int height, Hashtable hints) throws UnsupportedEncodingException, WriterException { PDF417Writer pdf417Writer = new PDF417Writer(); return pdf417Writer.encode(new String(content.getBytes("GBK"),"ISO-8859-1"), BarcodeFormat.PDF_417, 100, 50);//width、height为像素值 /* return pdf417Writer.encode(new String(content.getBytes("GBK"),"ISO-8859-1"), BarcodeFormat.PDF_417, width, height,hints);//width、height为像素值 */ } } }
需要的jar包 zxing-core-3.0.0-6.jar zxing-j2se-1.7.jar 永久有效
链接: https://pan.baidu.com/s/1vCA4nX1x830rUY4kbbte0A 提取码: wsax 复制这段内容后打开百度网盘手机App,操作更方便哦