|
Here is a screenshot of the code, you can refer to it to output the picture, but here is generated jpg
public static void drawImg () {
try {
int w = 0, h = 0, o_w = 0, o_h = 0, l = 0, t = 0;
// w- width of target image, h- height of target image, o_w- width of image to be cropped, o_h- height of image to be cropped, l- distance of cropped portion to the left of image to be cropped, t- distance of cropped portion to be cropped Image top margin
BufferedImage image = new BufferedImage (w, h,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics ();
Image img = Toolkit.getDefaultToolkit (). GetImage ("c:\\test.jpg"); // Get the source picture
System.out.println ("img-width =" + img.getWidth (null));
System.out.println ("img-height =" + img.getHeight (null));
g.setColor (Color.white); // The background color of the picture is white
g.fillRect (0, 0, w, h); // Draw a white rectangle first
g.drawImage (img, 0, 0, o_w, o_h, null); // First set the picture to the current size
g.dispose ();
g.drawImage (img, 0, 0, w, h, l, t, (l + w), (h + t), Color.WHITE,
null); // Crop
g.dispose ();
FileOutputStream outfile = new FileOutputStream ("d:\\test1.jpg"); // Output picture
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (outfile);
encoder.encode (image); // Output picture
outfile.close ();
} catch (Exception e) {
e.printStackTrace ();
}
} |
|