OAF页面集成条形码或者二维码

时间:2023-12-25 23:14:49

OAF页面集成条形码

OAF生成条形码

OAF页面集成二维码跟这个类似,生成二维码需要以下jar包,zxing-core.jar, zxing-javase.jar,可自行去maven下载。

代码如下:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*; import java.util.HashMap;
import java.util.Map; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeServlet extends HttpServlet
{ /**
*
*/
private static final long serialVersionUID = 1L; private String data = "NONE";
private String level = "M";
private int width = 200; public QRCodeServlet()
{
} public void init()
throws ServletException
{
} private void getParameter(HttpServletRequest httpservletrequest)
{
try
{
//httpservletrequest.setCharacterEncoding("iso-8859-1");
String val = httpservletrequest.getParameter("data");
val = new String(val.getBytes("iso-8859-1"), "utf-8");
setParameter("data", val); if(httpservletrequest.getParameter("width") != null)
{
setParameter("width", httpservletrequest.getParameter("width"));
}
if(httpservletrequest.getParameter("level") != null)
{
setParameter("level", httpservletrequest.getParameter("level"));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
} public void setParameter(String name, String value)
{
if(value != null)
if(name.equals("data"))
data = value;
else
if(name.equals("width"))
width = (new Integer(value)).intValue();
else
if(name.equals("level"))
level = value.toUpperCase();
} public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
getParameter(httpservletrequest); // httpservletrequest.setCharacterEncoding("UTF-8");
// httpservletresponse.setCharacterEncoding("UTF-8");
httpservletresponse.setContentType("image/png");
httpservletresponse.setHeader("Pragma", "no-cache");
httpservletresponse.setHeader("Cache-Control", "no-cache");
httpservletresponse.setDateHeader("Expires", 0L); try {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); if ("L".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
}
else if ("M".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
}
else if ("Q".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
}
else if ("H".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
}
else {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
} hints.put(EncodeHintType.CHARACTER_SET, "iso-8859-1"); BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(data
.getBytes("GBK"), "iso-8859-1"), BarcodeFormat.QR_CODE, width, width, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", httpservletresponse.getOutputStream());
httpservletresponse.getOutputStream().flush();
httpservletresponse.getOutputStream().close(); } catch (WriterException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException
{
try
{
doGet(httpservletrequest, httpservletresponse);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

配置servlet
下载
$ORA_CONFIG_HOME/10.1.3/j2ee/oacore/application-deployments/oacore/html/orion-web.xml
添加如下内容:

<!-- QRCode Servlet -->

  <servlet>
<servlet-name>QRCode</servlet-name>
<display-name>QRCode</display-name>
<servlet-class>oracle.apps.fnd.test.QRCodeServlet</servlet-class>
</servlet> <!-- QRCodeServlet mapping --> <servlet-mapping>
<servlet-name>QRCode</servlet-name>
<url-pattern>/QRCode</url-pattern>
</servlet-mapping>

调用
http://xxx.xxx.com.cn:8000/OA_HTML/QRCode?data=123567890&width=300&level=H
也可以参考前文中调用条形码的方式将其集成至OAF页面。

附:带logo展示二维码

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException; import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.ImageIcon; import java.util.HashMap;
import java.util.Map; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeServlet extends HttpServlet
{ /**
*
*/
private static final long serialVersionUID = 1L; private String data = "NONE";
private String level = "M";
private int width = 200;
private String charset = "GB2312"; private String logoflag = "N";
private String logopath = "//u02//prod14//appprod14//apps//apps_st//comn//java//classes//oracle//apps//media//logo.png";
private int logowidth = 40; private String removeborder = "N"; public QRCodeServlet()
{
} public void init()
throws ServletException
{
} private void getParameter(HttpServletRequest httpservletrequest)
{
try
{
httpservletrequest.setCharacterEncoding(charset);
String val = httpservletrequest.getParameter("data");
//val = new String(val.getBytes("gb2312"), "UTF-8");
setParameter("data", val); if(httpservletrequest.getParameter("width") != null)
{
setParameter("width", httpservletrequest.getParameter("width"));
}
if(httpservletrequest.getParameter("level") != null)
{
setParameter("level", httpservletrequest.getParameter("level"));
}
if(httpservletrequest.getParameter("logowidth") != null)
{
setParameter("logowidth", httpservletrequest.getParameter("logowidth"));
}
if(httpservletrequest.getParameter("logopath") != null)
{
setParameter("logopath", httpservletrequest.getParameter("logopath"));
}
if(httpservletrequest.getParameter("logoflag") != null)
{
setParameter("logoflag", httpservletrequest.getParameter("logoflag"));
} }
catch(Exception ex)
{
ex.printStackTrace();
}
} public void setParameter(String name, String value)
{
if(value != null)
if(name.equals("data"))
data = value;
else
if(name.equals("width"))
width = (new Integer(value)).intValue();
else
if(name.equals("level"))
level = value.toUpperCase();
else
if(name.equals("logowidth"))
logowidth = (new Integer(value)).intValue();
else
if(name.equals("logopath"))
logopath = value;
else
if(name.equals("logoflag"))
logoflag = value.toUpperCase();
else
if(name.equals("removeborder"))
removeborder = value.toUpperCase();
} public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
getParameter(httpservletrequest); httpservletresponse.setContentType("image/png");
// javax.servlet.ServletOutputStream servletoutputstream = httpservletresponse.getOutputStream();
httpservletresponse.setHeader("Pragma", "no-cache");
httpservletresponse.setHeader("Cache-Control", "no-cache");
httpservletresponse.setDateHeader("Expires", 0L); try {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); if ("L".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
}
else if ("M".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
}
else if ("Q".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
}
else if ("H".equals(level)) {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
}
else {
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
} hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, width, hints);
if ("N".equals(logoflag)) // don't display logo
{
MatrixToImageWriter.writeToStream(bitMatrix, "png", httpservletresponse.getOutputStream());
}
else // need display logo
{
ImageIcon icon = new ImageIcon(logopath); BufferedImage barcodeBI = MatrixToImageWriter.toBufferedImage(bitMatrix);
Image scaledImage = barcodeBI.getScaledInstance(256, 256, Image.SCALE_DEFAULT); BufferedImage bimage = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics(); g.drawImage(scaledImage, 0, 0, width, width, null);
g.drawImage(icon.getImage(), width/2-logowidth/2, width/2-logowidth/2, logowidth, logowidth, null);
g.dispose(); try {
ImageIO.write(bimage, "png", httpservletresponse.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
finally
{
barcodeBI.flush();
bimage.flush();
}
}
// MatrixToImageWriter.writeToStream(bitMatrix, "png", httpservletresponse.getOutputStream());
httpservletresponse.getOutputStream().flush();
httpservletresponse.getOutputStream().close(); } catch (WriterException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException
{
try
{
doGet(httpservletrequest, httpservletresponse);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

参考:Oracle中生成二维码图片_V1.1_WEB资料