睡不着... 逛论坛... 刚看见一个想要条形码的, 感觉有点意思, 模仿验证码写个例子
package test.plugin; import java.awt.image.BufferedImage; import java.io.IOException; import javax.servlet.ServletOutputStream; import org.krysalis.barcode4j.impl.code39.Code39Bean; import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; import org.krysalis.barcode4j.tools.UnitConv; import com.jfinal.kit.LogKit; import com.jfinal.render.Render; import com.jfinal.render.RenderException; /** * 生成条形码 - Barcode4J * * @author dufuzhong * @date 2017年8月1日 * jar : http://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j-light/2.0 * * 代码参考处: http://www.cnblogs.com/littleatp/p/4815921.html * 代码参考处: http://blog.csdn.net/wabiaozia/article/details/52747772 */ public class BarcodeRender extends Render { private String msg; BarcodeRender(String msg){ this.msg = msg; } // 自行扩展自己需要的业务 @Override public void render() { Code39Bean bean = new Code39Bean(); // 精细度 final int dpi = 150; // module宽度 final double moduleWidth = UnitConv.in2mm(1.0f / dpi); // 配置对象 bean.setModuleWidth(moduleWidth); bean.setWideFactor(3); bean.doQuietZone(false); String mime = "image/png"; response.setHeader("Pragma","no-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.setContentType(mime); ServletOutputStream sos = null; BitmapCanvasProvider canvas = null; try { sos = response.getOutputStream(); // 输出到流 canvas = new BitmapCanvasProvider(sos, mime, dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0); // 生成条形码 bean.generateBarcode(canvas, msg); // 结束绘制 canvas.finish(); } catch (IOException e) { if (getDevMode()) { throw new RenderException(e); } } catch (Exception e) { throw new RenderException(e); } finally { if (sos != null) { try {sos.close();} catch (IOException e) {LogKit.logNothing(e);} } } } }
测试一下: // /test/barcode/jfinal-20170801 @Clear() public class BarcodeController extends Controller { public void index(){ render(new BarcodeRender(getPara())); } }
23333, 解码就不写了