This weekend the teacher asked to do a treasure map effect of the picture IO test, the desired effect is to first give you four of the pictures have been processed by PS, these pictures for the treasure map of the four images intercepted.
The following four images are provided:
1.jpg
2.jpg
3.jpg
4.jpg
Ultimate hope for the effect of synthesis:
Attached code:
Package com.hcj.july19.am;
Import the required package import java.io.*;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage; * * When importing these two packages, MyEclipse will give an error: * Access restriction:the type JPEGImageEncoder is isn't accessible due to restriction on r
equired Library C:\Java\jre1.6.0_07\lib\rt.jar * Workaround * Eclipse defaults to setting these access-restricted APIs as error. * Just put the forbidden references in the deprecated and restricted APIs inside the windows-preferences-java-complicer-errors/warnings (
Access rules) is selected as warning and can be compiled.
* * Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder; public class Imagetest {public Imagetest () {} public static void Main (string[] args) {try {imagetest test = NE
W Imagetest (); Test.
Imagetset ();
catch (Exception e) {System.out.print (e); The public void Imagetset () throws Exception {//Create four file objects (note: The width of the four pictures here is the same, so the following definition bufferedimage width is the width of the first) file _ File1 = new File ("1.jpg ");
File _file2 = new file ("2.jpg");
File _file3 = new file ("3.jpg");
File _file4 = new file ("4.jpg");
Image Src1 = Javax.imageio.ImageIO.read (_file1);
Image src2 = Javax.imageio.ImageIO.read (_file2);
Image SRC3 = Javax.imageio.ImageIO.read (_FILE3);
Image SRC4 = Javax.imageio.ImageIO.read (_file4);
Gets the width of the picture int width = src1.getwidth (null);
Gets the height int height1 = src1.getheight (null) of each image;
int height2 = Src2.getheight (null);
int HEIGHT3 = Src3.getheight (null);
int HEIGHT4 = Src4.getheight (null); Constructs a bufferedimage type that is one of the predefined image types. Width is the first width, height for each picture height and bufferedimage tag = new BufferedImage (width, height1 + height2 + height3 + height4, BufferedImage.
TYPE_INT_RGB);
Create output stream FileOutputStream out = new FileOutputStream ("treasuremap.jpg");
Draw synthetic image Graphics G = tag.creategraphics ();
G.drawimage (src1, 0, 0, width, height1, null);
G.drawimage (src2, 0, height1, width, height2, null); G.drawimage (SRC3, 0, height1 + height2, width, HEIGHT3, nULL);
G.drawimage (SRC4, 0, height1 + height2 + height3, width, height4, null);
Releases the context of this graphic and all the system resources it uses.
G.dispose ();
Generates the drawn image to the output stream JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (tag);
Turn off the output stream out.close ();
System.out.println ("Hidden Treasure map Out");
}
}
Test pass, each picture is connected to normal, no fault phenomenon
Note: The above mentioned in the introduction of Jpegcodec and JPEGImageEncoder in eclipse will be an error (here specifically, this is a hint of eclipse, does not affect the operation of the program)
This side read the comments of the students to talk about this problem, I am here to demonstrate.
1. Before setting, the code floats red.
2. Move to the red part of the mouse, will appear prompt window, click on the link below
3. The following interface appears, click Apply, and then click OK
4. Setup Complete