June 18th 10:56 You have been studying Java since you were in junior high school. How amazing! hehe! !
This code should be a method in a Bean class. Its main function is to create a picture named kangzhw.jpg according to the picture you gave. That is to say, copy the picture you gave into kangzhw.jpg, and a few words were added to the copied file (it’s not clear what to add specifically).
----------------------------------------
Let’s look at it line by line:
Image img = ImageIO.read(new File(request.getRealPath("/")+"index\\"+fileName ));
Explanation :
//request.getRealPath("/"), get the root directory of the current site.
//Image img = ImageIO.read(), here is what you gave File name, read the file into the img object (the text should be a picture, I don’t know why there is no judgment here).
----------------- ------------------------------------------
int width = img.getWidth(null);
int height = img.getHeight(null);
Explanation:
//These two sentences are used for definition The width and height of the image. ! !
------------------------------------------------ ------------------
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Description: p>
Here is to open a buffer according to the defined image width and height for storing images in the future.
------------------------------------------------ ------------------
Graphics g = image.createGraphics();
g.drawImage(img, 0, 0 , width, height, null);
Explanation:
This is based on the defined width and height and the above img, create a brush (that is, Graphics), because the buffer area is Once it's opened, there has to be something to get something in there. image is the image buffer.
------------------------------------------------ ------------------
String stri = null;
//
if(addString .length()>14)
{
stri = addString.substring(1,14).concat("...");
} else
{
stri = addString;
}
//I don’t know what this sentence means, it just goes to the picture the string added in.
------------------------------------------------ ------------------
//Set the color to be painted to Green, GREEN is a constant.
g.setColor(Color.GREEN);
//Set the font size style. (With comments)
g.setFont(new Font("Courier", Font.PLAIN, width/10)); //Font, style, size settings
// The coordinates where the string and font are located. (With comments)
g.drawString(getStr(stri), width - width*3/4, height - height/2);
//Put the brush you just made Throw it away.
destroy!
g.dispose();
Explanation: All the above operations are to first open a picture buffer (that is, draw an empty picture in the memory), and then in this Add some words to the picture in the memory, so as to prepare a template, and then add the picture you gave to this picture in a while.
=====================
File tempFile= new File(request.getRealPath("/")+ "index\\" ,"kangzhw.jpg" );
Explanation: A new file object is created here. The file name is kangzhw.jpg. Here we just create an object.
Note: From here on, I feel there is something wrong with the code! So no explanation!
FileOutputStream os = new FileOutputStream(request.getRealPath("/")+"index\\"+"kangzhw.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os );
encoder.encode(image);
os.close();