1 ImportCom.google.zxing.BarcodeFormat;2 Importcom.google.zxing.WriterException;3 ImportCom.google.zxing.client.j2se.MatrixToImageWriter;4 ImportCom.google.zxing.common.BitMatrix;5 ImportCom.google.zxing.qrcode.QRCodeWriter;6 7 ImportJavax.servlet.ServletOutputStream;8 ImportJavax.servlet.http.HttpServletResponse;9 ImportJava.io.File;Ten One Public classCreateqrcode { A - - /** the * Generate two-dimensional code method - * - * @paramThe URL QR code represents the data - * @paramResp Response Object + * @throwsException throws an exception - */ + Public voidGetqrcode (String URL, HttpServletResponse resp)throwsException { AServletoutputstream stream =NULL; at Try { - -String format = "JPG";//picture format of QR code -File OutputFile =NewFile ("D:" + File.separator + "new.jpg"); -stream =Resp.getoutputstream (); -Qrcodewriter Qrcodewriter =NewQrcodewriter (); inBitmatrix BM = qrcodewriter.encode (URL, Barcodeformat.qr_code, 300, 300); -Matrixtoimagewriter.writetostream (BM, "PNG", stream); to matrixtoimagewriter.writetofile (BM, format, outputFile); +}Catch(writerexception e) { - e.getstacktrace (); the}finally { * if(Stream! =NULL) { $ Stream.flush ();Panax Notoginseng stream.close (); - } the } + } A}
Java generates two-dimensional code