JAVA zoom image, image cut, change colors, change the type
Advertisements
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import javax.imageio.ImageIO;
public class ImageCut {
/**
* Zoom Image
*
* @param srcImageFile
* Address of the source image file
* @param result
* Address of a zoomed image
* @param scale
* Scaling
* @param flag
* Zoom Select :true Enlarge ; false Narrow ;
*/
public static void scale(String srcImageFile, String result, int scale,
boolean flag) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // Read the file
int width = src.getWidth(); // Get source image width
int height = src.getHeight(); // Long been the source map
if (flag) {
// Enlarge
width = width * scale;
height = height * scale;
} else {
// Narrow
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // After drawing the plan reduced
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// Output to the file stream
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Image Cut
*
* @param srcImageFile
* Source image address
* @param descDir
* Slice the destination folder
* @param destWidth
* Target Chip width
* @param destHeight
* Target Chip Height
*/
public static void cut(String srcImageFile, String descDir, int destWidth,
int destHeight) {
try {
Image img;
ImageFilter cropFilter;
// Read the source image
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // Source image width
int srcHeight = bi.getWidth(); // Source image height
if (srcWidth > destWidth && srcHeight > destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
destWidth = 200; // Slice width
destHeight = 150; // Slice height
int cols = 0; // The number of horizontal slices
int rows = 0; // The number of vertical slices
// Calculate the number of horizontal and vertical slices
if (srcWidth % destWidth == 0) {
cols = srcWidth / destWidth;
} else {
cols = (int) Math.floor(srcWidth / destWidth) + 1;
}
if (srcHeight % destHeight == 0) {
rows = srcHeight / destHeight;
} else {
rows = (int) Math.floor(srcHeight / destHeight) + 1;
}
// Circulation of the slice
// Ideas for improvement : Multi-threading to speed up the availability of cutting speed
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// Four parameters are the starting point coordinates and width and height of image
// That : CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * 200, i * 150,
destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // After drawing the plan reduced
g.dispose();
// Output file
ImageIO.write(tag, "JPEG", new File(descDir
+ "pre_map_" + i + "_" + j + ".jpg"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Image type conversion
* GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
*/
public static void convert(String source, String result) {
try {
File f = new File(source);
f.canRead();
f.canWrite();
BufferedImage src = ImageIO.read(f);
ImageIO.write(src, "JPG", new File(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Color to black and white
*
* @param source
* @param result
*/
public static void gray(String source, String result) {
try {
BufferedImage src = ImageIO.read(new File(source));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, "JPEG", new File(result));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
//cut("C:/1.jpg", "C:/test/", 200, 150);
}
}
Related Posts of JAVA zoom image, image cut, change colors, change the type
-
JDBC example of a long time do not have JDBC forgot
A back-up here to stay. The first: The second:
-
JAVA interview questions
JAVA interview questions 1, object-oriented features of what has 1. Abstract 2. Inheritance 3. Packaging 4. Polymorphisms 2, String data types are the most basic right? Basic data types include byte, int, char, long, float, double, boolean and short. java
-
JAVA EE JSP_JNDI
dsfdsa http://lindows.javaeye.com/admin/blogs/213348 Tomcat 6 with the connection pool data source configuration http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html project: test Driver path: D: \ workspace \ test \ WebRoot \ WEB-INF \ lib ...
-
In the servlet use Bean
According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q












