条码处理 @author SeanDragon
| 29 | * @author SeanDragon |
| 30 | */ |
| 31 | public final class ToolBarCode { |
| 32 | |
| 33 | private ToolBarCode() { |
| 34 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * 生成二维码 |
| 39 | * |
| 40 | * @param content 条码文本内容 |
| 41 | * @param width 条码宽度 |
| 42 | * @param height 条码高度 |
| 43 | * @param fileType 文件类型,如png |
| 44 | * @param savePath 保存路径 |
| 45 | */ |
| 46 | @SuppressWarnings({"rawtypes", "unchecked", "deprecation"}) |
| 47 | public static void encode(String content, int width, int height, String fileType, String savePath) throws IOException, WriterException { |
| 48 | Charset charset = Charset.forName("UTF-8"); |
| 49 | CharsetEncoder encoder = charset.newEncoder(); |
| 50 | |
| 51 | byte[] b = encoder.encode(CharBuffer.wrap(content)).array(); |
| 52 | String data = new String(b, "iso8859-1"); |
| 53 | |
| 54 | Writer writer = new QRCodeWriter(); |
| 55 | BitMatrix matrix = writer.encode(data, QR_CODE, width, height); |
| 56 | MatrixToImageWriter.writeToPath(matrix, fileType, Paths.get(savePath)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * 生成带logo的二维码 |
| 62 | * |
| 63 | * @param content 条码文本内容 |
| 64 | * @param width 条码宽度 |
| 65 | * @param height 条码高度 |
| 66 | * @param logoPath 条码中logo的路径 |
| 67 | * @param fileType 文件类型,如png |
| 68 | * @param savePath 保存路径 |
| 69 | */ |
| 70 | public static void encodeLogo(String content, int width, int height, String logoPath, String fileType, String savePath) throws IOException, WriterException { |
| 71 | Charset charset = Charset.forName("UTF-8"); |
| 72 | CharsetEncoder encoder = charset.newEncoder(); |
| 73 | |
| 74 | byte[] b = encoder.encode(CharBuffer.wrap(content)).array(); |
| 75 | String data = new String(b, "iso8859-1"); |
| 76 | |
| 77 | Writer writer = new QRCodeWriter(); |
| 78 | BitMatrix matrix = writer.encode(data, QR_CODE, width, height); |
| 79 | MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLACK, 10); |
| 80 | MatrixToImageWriterEx.writeToFile(matrix, fileType, savePath, logoPath, logoConfig); |
| 81 | |
| 82 | |
| 83 | // BitMatrix matrix = MatrixToImageWriterEx.createQRCode(content, width, height); |
| 84 | // MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLUE, 4); |
| 85 | // MatrixToImageWriterEx.writeToFile(matrix, fileType, savePath, logoPath, logoConfig); |
| 86 | } |
| 87 | |
| 88 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected