Java draws a picture and produces a preview of the image file before printing, which is suitable for the development phase. If you want to use the interface to complete the preview, please find another resource.
This feature is used primarily for results previews, because a lot of paper is wasted if you use printing to validate results.
Java code:
Import Java.awt.Color;
Import java.awt.Dimension;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.Rectangle;
Import Java.awt.geom.AffineTransform;
Import Java.awt.image.BufferedImage;
Import Java.awt.print.PageFormat;
Import java.awt.print.Printable;
Import java.awt.print.PrinterException;
Import Java.awt.print.PrinterJob;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Date;
Import Javax.imageio.ImageIO;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGEncodeParam;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
Import Com.synnex.reception.util.PLBarCode39;
Import Com.synnex.reception.vo.VisitInfoVo;
public class Printtest {
public static void Main (string[] args) {
String BarCode = "201408210001";
String datestr = "2014/08/25 23:59:59";
String name = "Christopher Adam Hernandez Sotelo";
String visitorcompany = "China Chengdu";
String EmployeeName = "Marquisha Ardell eddington-williams";
int zoom = 6;
BufferedImage resultimage = null;
BufferedImage bufferedimage = new BufferedImage (216*ZOOM,144*ZOOM,BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = bufferedimage.getgraphics ();
G.draw3drect (0, 0, 216, 144, true);
G.setcolor (Color.White);
G.fillrect (0, 0, 216*zoom, 144*zoom);
G.setcolor (Color.Black);
Image Image;
try {
Image = Imageio.read (PrintTest.class.getClassLoader (). GetResource ("Logo.bmp"). OpenStream ());
if (image! = null) {
G.drawimage (image, 6*zoom, 6*zoom, 60*zoom, 24*zoom, NULL);
}
G.setfont (New Font ("Arial", Font.Bold, 14*zoom));
g.DrawString ("VISITOR", 100*zoom, 24*zoom);
G.setfont (New Font ("Arial", Font.Bold, 10*zoom));
g.DrawString ("Name:", 61*zoom, 42*zoom);
g.DrawString ("Entry Time:", 37*zoom,63*zoom);
g.DrawString ("Company Name:", 12*zoom,80*zoom);
g.DrawString ("Employee Name:", 10*zoom,97*zoom);
G.setfont (New Font ("Arial", Font.plain, 8*zoom));
g.DrawString (Name,92*zoom, 42*zoom);
g.DrawString (Datestr, 92*zoom,63*zoom);
g.DrawString (Visitorcompany, 92*zoom,80*zoom);
g.DrawString (EmployeeName, 92*zoom,97*zoom);
G.drawimage (Generatebarcodeimage (BarCode), 40*zoom, 112*zoom, 152*zoom, 30*zoom, NULL);
G.dispose ();
Resultimage = Bufferedimage;//rotate (BufferedImage, 360);
Encode:com.sun.image.codec.jpeg
Bufferedoutputstream out = new Bufferedoutputstream (new FileOutputStream (New File ("c:\\test.jpg"));
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
JPEGEncodeParam param = Encoder.getdefaultjpegencodeparam (resultimage);
Param.setquality (1.0f, false);
Encoder.setjpegencodeparam (param);
try {
Encoder.encode (Resultimage);
}catch (IOException IoE) {
Ioe.printstacktrace ();
}
}catch (Exception e) {
E.printstacktrace ();
}
//
Print (resultimage);
}
}
The above code is mainly to draw the picture you want to produce, and then output the picture to a file, manually open the file to see the results. The following code is for exporting pictures to disk:
Java code:
Encode:com.sun.image.codec.jpeg
Bufferedoutputstream out = new Bufferedoutputstream (new FileOutputStream (New File ("c:\\test.jpg"));
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
JPEGEncodeParam param = Encoder.getdefaultjpegencodeparam (resultimage);
Param.setquality (1.0f, false);
Encoder.setjpegencodeparam (param);
try {
Encoder.encode (Resultimage);
}catch (IOException IoE) {
Ioe.printstacktrace ();
}
Java Draw picture Create picture file to preview