Package Com.zte.evs.ebill.common;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.qrcode.QRCodeWriter;
/** * Class Header number: * Class name: Barcodeimage * Content Summary: Based on barcode string generated barcode Map * * Public class Barcodeimage {private int barwidth;//set bar code width private int barheight;//Set bar code height private int left;//set left margin private int top;//set bar code distance from top/** * @param logica Lbarcode Logical code * @param g brush */private void buildimg (String logicalbarcode, Graphics g) {for (int i = 0; I < logicalbarcode.length ();
i++) {Char cc = Logicalbarcode.charat (i);
if (cc = = ' 1 ') paint (g, color.black, left);
else Paint (g, color.white, left);
Left + + barwidth; }/** * @param g brush * @param color Barcode Bar Colour * @param left barcode Bar (* * * * * * */private void Paint (Graphics g, Color color, int left) {g.setcolor (color);
G.fillrect (left, top, barwidth, barheight); /** * Generate Barcode Diagram Entry method * @param codestr: Barcode String * @param barwidth: Barcode Width * @param barheight: Barcode Height * @param left: Bar code @param top: Bar-code margin or margin * @return return bufferedimage Object */Public BufferedImage Buil
Dcodebarimg (String codestr,int barwidth, int barheight, int left, int top) {BufferedImage image=null;
Graphics G=null;
try {boolean isrigth=checkparameter (codestr,barwidth,barheight,left,top);
if (isrigth) {BarCode128 code128=new BarCode128 ();
String logicalbarcode= Code128.getlogicalbarcode (CODESTR);
int imagewidth=logicalbarcode.length () *barwidth+ (2*left);
int imagehieght=barheight+ (2*top);
Image = New BufferedImage (ImageWidth, Imagehieght, Bufferedimage.type_int_rgb);
g = Image.getgraphics ();
G.setcolor (Color.White); G.fillrect (0, 0, imagewidth, imagehieght); Buildimg (logicalbarcode,g);//Build bar code diagram} else {image = new BufferedImage (MB, Bufferedi Mage.
TYPE_INT_RGB);
g = Image.getgraphics ();
G.setcolor (Color.White);
g.DrawString ("Create Barcode graph failed!", 20, 80); The catch (Exception e) {image = new bufferedimage, buffered
IMAGE.TYPE_INT_RGB);
g = Image.getgraphics ();
G.setcolor (Color.White);
g.DrawString ("Create Barcode graph failed!", 20, 80);
E.printstacktrace ();
} return image; BufferedImage builddatamaximg (String codestr,int barwidth, int barheight, int left, int top) {Bufferedima
GE Image=null;
Graphics G=null;
try {boolean isrigth=checkparameter (codestr,barwidth,barheight,left,top); if (isrigth) {image = new BufferedImage (BarWidth, Barheight, bufferedimage.tYPE_INT_RGB);
Bitmatrix Bitmatrix = new Qrcodewriter (). Encode (Codestr, Barcodeformat.qr_code, BarWidth, barheight); for (int x = 0; x < BarWidth + +) {for (int y = 0; y < barheight; y++) {Image.setrgb (x, Y, Bitmatrix . get (x, y) = = true?
BarCodeServlet.BLACK:BarCodeServlet.WHITE);
}} else {image = new BufferedImage (BUFFEREDIMAGE.TYPE_INT_RGB);
g = Image.getgraphics ();
G.setcolor (Color.White);
g.DrawString ("Create two-dimensional code map failed!", 20, 80);
The catch (Exception e) {image = new bufferedimage, BUFFEREDIMAGE.TYPE_INT_RGB);
g = Image.getgraphics ();
G.setcolor (Color.White);
g.DrawString ("Create two-dimensional code map failed!", 20, 80);
E.printstacktrace ();
} return image; /** * Check the legality of the parameter * @param codestr: Barcode String * @param barwidth: Barcode Width * @param barheight: Barcode Height * @param left: bar code @param top: bar code margin or margin * @return Boolean
* */Private Boolean checkparameter (String codestr,int barwidth, int barheight, int left, int top) {Boolean
Isrigth=true;
try {this.barwidth = barwidth;
This.barheight = Barheight;
This.left = left;
This.top = top; if (codestr==null| |
Codestr.length () <=0) {isrigth=false;
throw new Exception ("barcode string cannot be empty!");
else if (barwidth<=0) {isrigth=false;
throw new Exception ("Barcode line width cannot be less than or equal to 0!");
else if (barheight<=0) {isrigth=false;
throw new Exception ("Barcode line height cannot be less than equal to 0!");
else if (left<0) {isrigth=false;
throw new Exception ("barcode line left margin cannot be less than 0!");
else if (top<0) {isrigth=false;
throw new Exception ("barcode line top margin cannot be less than 0!");
} catch (Exception e) {e.printstacktrace ();
return isrigth;
////public static void main (string[] args) throws Exception {/////Barcodeimage barcodeimage=new (); BufferedImage imaGe=barcodeimage.buildcodebarimg ("FDSAFSD", 2,100,0,0);
Jpegcodec.createjpegencoder (New FileOutputStream ("C:/jamie7.jpg")). Encode (image);
// }
}