📄 idautomationservlet.java
字号:
//*****************************************************************
//
// JAVA Source for com.idautomation.datamatrix; 4.10
//
// Copyright, IDAutomation.com, Inc. 2000-2004.
// All rights reserved.
//
// http://www.IDAutomation.com/java/
//
// NOTICE:
// You may incorporate our Source Code in your application
// only if you own a valid Java Developer License
// from IDAutomation.com, Inc. and the copyright notices
// are not removed from the source code.
//
//*****************************************************************
package com.idautomation.barcode.datamatrix;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.Graphics2D.*;
import java.awt.*;
import com.idautomation.barcode.encoder.GifEncoder;
public class IDAutomationServlet extends HttpServlet
{
private boolean debug=false;
public void init() throws ServletException { }
// MODIFY THIS FUNCTION TO CREATE THE DataMatrix USING THE request PARAMETERS
private DataMatrix getChart (HttpServletRequest request)
{
DataMatrix cb=new DataMatrix();
if (request!=null)
{
// You may remark these parameters out if you do not need them.
String st = null;
if (request.getParameter("PFMT")!=null)
{
st = (request.getParameter("PFMT"));
if (st.toUpperCase().compareTo("C10X10")==0) cb.preferredFormat=cb.C10X10;
if (st.toUpperCase().compareTo("C12X12")==0) cb.preferredFormat=cb.C12X12;
if (st.toUpperCase().compareTo("C14X14")==0) cb.preferredFormat=cb.C14X14;
if (st.toUpperCase().compareTo("C16X16")==0) cb.preferredFormat=cb.C16X16;
if (st.toUpperCase().compareTo("C18X18")==0) cb.preferredFormat=cb.C18X18;
if (st.toUpperCase().compareTo("C20X20")==0) cb.preferredFormat=cb.C20X20;
if (st.toUpperCase().compareTo("C22X22")==0) cb.preferredFormat=cb.C22X22;
if (st.toUpperCase().compareTo("C24X24")==0) cb.preferredFormat=cb.C24X24;
if (st.toUpperCase().compareTo("C26X26")==0) cb.preferredFormat=cb.C26X26;
if (st.toUpperCase().compareTo("C32X32")==0) cb.preferredFormat=cb.C32X32;
if (st.toUpperCase().compareTo("C36X36")==0) cb.preferredFormat=cb.C36X36;
if (st.toUpperCase().compareTo("C40X40")==0) cb.preferredFormat=cb.C40X40;
if (st.toUpperCase().compareTo("C44X44")==0) cb.preferredFormat=cb.C44X44;
if (st.toUpperCase().compareTo("C48X48")==0) cb.preferredFormat=cb.C48X48;
if (st.toUpperCase().compareTo("C52X52")==0) cb.preferredFormat=cb.C52X52;
if (st.toUpperCase().compareTo("C64X64")==0) cb.preferredFormat=cb.C64X64;
if (st.toUpperCase().compareTo("C72X72")==0) cb.preferredFormat=cb.C72X72;
if (st.toUpperCase().compareTo("C80X80")==0) cb.preferredFormat=cb.C80X80;
if (st.toUpperCase().compareTo("C88X88")==0) cb.preferredFormat=cb.C88X88;
if (st.toUpperCase().compareTo("C96X96")==0) cb.preferredFormat=cb.C96X96;
if (st.toUpperCase().compareTo("C104X104")==0) cb.preferredFormat=cb.C104X104;
if (st.toUpperCase().compareTo("C120X120")==0) cb.preferredFormat=cb.C120X120;
if (st.toUpperCase().compareTo("C132X132")==0) cb.preferredFormat=cb.C132X132;
if (st.toUpperCase().compareTo("C144X144")==0) cb.preferredFormat=cb.C144X144;
if (st.toUpperCase().compareTo("C8X18")==0) cb.preferredFormat=cb.C8X18;
if (st.toUpperCase().compareTo("C8X32")==0) cb.preferredFormat=cb.C8X32;
if (st.toUpperCase().compareTo("C12X26")==0) cb.preferredFormat=cb.C12X26;
if (st.toUpperCase().compareTo("C12X36")==0) cb.preferredFormat=cb.C12X36;
if (st.toUpperCase().compareTo("C16X36")==0) cb.preferredFormat=cb.C16X36;
if (st.toUpperCase().compareTo("C16X48")==0) cb.preferredFormat=cb.C16X48;
}
if (request.getParameter("MODE")!=null)
{
st = (request.getParameter("MODE"));
if (st.toUpperCase().compareTo("ASCII")==0) cb.encoding=cb.E_ASCII;
if (st.toUpperCase().compareTo("C40")==0) cb.encoding=cb.E_C40;
if (st.toUpperCase().compareTo("TEXT")==0) cb.encoding=cb.E_TEXT;
if (st.toUpperCase().compareTo("BASE256")==0) cb.encoding=cb.E_BASE256;
if (st.toUpperCase().compareTo("AUTO")==0) cb.encoding=cb.E_BASE256;
}
if (request.getParameter("ROTATE")!=null) cb.rotate=new Integer(request.getParameter("ROTATE")).intValue();
if (request.getParameter("MARGIN")!=null) cb.marginCM=new Double(request.getParameter("MARGIN")).doubleValue();
if (request.getParameter("X")!=null) cb.X=new Double(request.getParameter("X")).doubleValue();
if (request.getParameter("PT")!=null) cb.processTilde=(request.getParameter("PT").compareTo("Y")==0);
if (request.getParameter("BARCODE")!=null)
{
cb.code=request.getParameter("BARCODE");
}
else
{
cb.code="NO DATA TO ENCODE";
}
}
else
{
cb.code="NO DATA TO ENCODE";
}
return cb;
}
// Handle a request
// 1. create DataMatrix
// 2. draw DataMatrix in a Buffered Image
// 3. encode image as GIF or JPEG and send it to the browser
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
ServletOutputStream outb;
String encode="jpeg";
if (request!=null)
{
if (request.getParameter("FORMAT")!=null) encode=request.getParameter("FORMAT").toLowerCase();
if (encode.compareTo("gif")!=0) encode="jpeg";
}
response.setContentType("image/"+encode);
outb=response.getOutputStream();
// avoid caching in browser
response.setHeader ("Pragma", "no-cache");
response.setHeader ("Cache-Control", "no-cache");
response.setDateHeader ("Expires",0);
try
{
// Create buffer
//the default height and width if not specified.
int w=80;
int h=80;
// get DataMatrix
DataMatrix cb=getChart(request);
if ((request!=null) && (request.getParameter("WIDTH")!=null) && (request.getParameter("HEIGHT")!=null))
{
w=new Integer(request.getParameter("WIDTH")).intValue();
h=new Integer(request.getParameter("HEIGHT")).intValue();
}
else
{
//a temp image must be created to find the preferred size
cb.autoSize=true;
cb.setSize(90,90);
java.awt.image.BufferedImage imageTemp = new java.awt.image.BufferedImage( cb.getSize().width,cb.getSize().height,java.awt.image.BufferedImage.TYPE_BYTE_INDEXED );
java.awt.Graphics imgTempGraphics = imageTemp.createGraphics();
cb.paint(imgTempGraphics);
cb.invalidate();
w=cb.getSize().width;
h=cb.getSize().height;
imgTempGraphics.dispose();
}
java.awt.image.BufferedImage BarImage=new java.awt.image.BufferedImage(w,h,java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Graphics2D BarGraphics=BarImage.createGraphics();
cb.setSize(w,h);
cb.paint(BarGraphics);
if (encode.compareToIgnoreCase("gif")==0)
{
// encode buffered image to a gif
GifEncoder encoder = new GifEncoder(BarImage ,outb);
encoder.encode();
}
else
{
// create JPEG image
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(outb );
//increase the JPEG quality to 100%
com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam( BarImage);
param.setQuality(1.0F,true);
encoder.setJPEGEncodeParam(param);
encoder.encode( BarImage,param );
}
}
catch (Exception e) { e.printStackTrace();}
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException
{
try
{
doGet(request,response);
}
catch (Exception e) { e.printStackTrace();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -