Java implementation of the site to upload pictures stamped method
- 2020-04-01 04:02:56
- OfStack
This article describes the example of the Java implementation of the site to upload pictures stamped method. Share with you for your reference. The details are as follows:
Recently boring, on the alumni record for a while, feel the alumni record of the picture will add a chapter, ha ha, he also made a, but only suitable for JPG format. Send it out for research. Welcome to discuss!
Very old code
/************************************************
* <p>java Manipulation of images ( Can only use jpg)</p>
* Stamp the picture <br>
* A reduction of a picture <br>
* <p>Title:java Manipulation of images ( Can only use jpg)</p>
* <p>CreateData: 2004-12-2</p>
* <p>Description:</p>
* <p>Copyright: Copyright (c) 2004</p>
* @author wang
* @version 1.0
***********************************************/
package com.cn.wangk.test;
import java.io.*;
import com.sun.image.codec.jpeg.*;//Sun only provides the encoding API for JPG image files
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import javax.imageio.ImageIO;
public class Test {
public Test() {
try {
//Generate a new image address
File fo = new File("c:\4.jpg");
//Read the image file
String imagePath = "C:\Documents and Settings\Administrator"
+ "\My Documents\My Pictures\1.jpg";
//Stamped picture file
String toimagepth = "C:\1.jpg";
//Get the file stream for the image
InputStream imageIn;
imageIn = new FileInputStream(new File(imagePath));
//Get the input encoder, the file stream for JPG format encoding
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
//Get the encoded image object
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g = image.getGraphics();
try {
InputStream imageIn2 = null;
imageIn2 = new FileInputStream(new File(toimagepth));
//Get the input encoder, the file stream for JPG format encoding
JPEGImageDecoder decoder2 = JPEGCodec
.createJPEGDecoder(imageIn2);
//Get the encoded image object
BufferedImage image2 = decoder2.decodeAsBufferedImage();
//Picture stamp
ImageObserver obser = null;
int x = image.getWidth() - image2.getWidth();
int y = image.getHeight() - image2.getHeight();
g.drawImage(image2, x, y, obser);
} catch (FileNotFoundException e) {
//Failure to open the file means that the seal picture does not exist. In this case, directly stamp the file seal (signature).
g.setFont(new Font(" Song typeface ", Font.PLAIN, 18));
g.drawString(" Qiushui studio ", image.getWidth() - 100,
image.getHeight() - 20);
g.drawString("water_wang@xs.zj.cn", image.getWidth() - 180,
image.getHeight() - 10);
}
g.dispose();
ImageIO.write(image, "jpeg", fo);
System.out.println("ok");
} catch (FileNotFoundException e) {
//Automatically generate catch blocks
e.printStackTrace();
} catch (ImageFormatException e) {
//Automatically generate catch blocks
e.printStackTrace();
} catch (IOException e) {
//Automatically generate catch blocks
e.printStackTrace();
}
}
public static void saveFixedBoundIcon(File imageFile, int height, int width)
throws Exception {
double Ratio = 0.0;
if (imageFile == null || !imageFile.isFile())
throw new Exception(imageFile + " The specified file could not be found !");
String filePath = imageFile.getPath();
BufferedImage Bi = ImageIO.read(imageFile);
if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
if (Bi.getHeight() > Bi.getWidth()) {
Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
} else {
Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
}
File savefile = new File(filePath + "_" + height + "_" + width
+ ".jpg");
Image Itemp = Bi.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp, "jpeg", savefile);
} catch (Exception ex) {
}
}
}
public static void main(String[] args) {
// Test ts = new Test();
try {
Test.saveFixedBoundIcon(new File(
"C:\test.jpg"), 200, 200);
} catch (Exception e) {
//Automatically generate catch blocks
e.printStackTrace();
}
}
}
I hope this article has been helpful to your Java programming.