java图片处理的代码,呵呵!!
写道
/**
* 图片缩放工具类
*/
package com.wanda.film.mobile.util;
import java.io.*;
import java.util.Calendar;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageUtils {
/**
* 缩放
*
* @param filePath
* 图片路径
* @param height
* 高度
* @param width
* 宽度
* @param bb
* 比例不对时是否需要补白
*/
public static void resize(String filePath, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 缩放比例
File f = new File(filePath); // 文件路径
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
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();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void scale(String srcImageFile, String result, int tagwidth,
int tagheight) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
// if (tagwidth > width && tagheight > height) {
// return;
// }
// 图片尺寸计算
double tsx = (double) width / tagwidth;
double tsy = (double) height / tagheight;
double theheight = height / tsx;
if ((int) Math.round(theheight) > tagheight) {
double thewidth = width / tsy;
tagwidth = (int) Math.round(thewidth);
} else {
tagheight = (int) Math.round(theheight);
}
/* 用于AffineTransform.getScaleInstance返回表示缩放变换的变换。 */
double sx = (double) tagwidth / src.getWidth();
double sy = (double) tagheight / src.getHeight();
/*
* TYPE_INT_RGB:表示一个图像,具有打包成整数像素的 8 位 RGB 颜色分量
* TYPE_INT_ARGB:比RGB多了透明度的alpha值
*/
BufferedImage tag = new BufferedImage(tagwidth, tagheight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = tag.createGraphics();
/* 为呈现算法设置单个首选项的值 */
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
/* 呈现 RenderedImage,在绘制前进行从图像空间到用户空间的转换 */
g.drawRenderedImage(src, AffineTransform.getScaleInstance(sx, sy));
/* 释放此图形的上下文并释放它所使用的所有系统资源 */
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试图片缩放
public static void main(String[] args) throws IOException {
// ImageUtils.resize("D:\\photo\\005.jpg", 480, 320,true);
Calendar calendar = Calendar.getInstance();
long startTime = calendar.getTimeInMillis();
// 测试图片转换
ImageUtils.scale("D:\\photo\\12.jpg", "D:\\photo\\12test.jpg", 39, 39);
Calendar calendar1 = Calendar.getInstance();
long endTime = calendar1.getTimeInMillis();
// 测试图片转换的时间
System.out.println("图片转换的时间: " + String.valueOf(endTime - startTime)
+ "毫秒");
}
}
* 图片缩放工具类
*/
package com.wanda.film.mobile.util;
import java.io.*;
import java.util.Calendar;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageUtils {
/**
* 缩放
*
* @param filePath
* 图片路径
* @param height
* 高度
* @param width
* 宽度
* @param bb
* 比例不对时是否需要补白
*/
public static void resize(String filePath, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 缩放比例
File f = new File(filePath); // 文件路径
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
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();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void scale(String srcImageFile, String result, int tagwidth,
int tagheight) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
// if (tagwidth > width && tagheight > height) {
// return;
// }
// 图片尺寸计算
double tsx = (double) width / tagwidth;
double tsy = (double) height / tagheight;
double theheight = height / tsx;
if ((int) Math.round(theheight) > tagheight) {
double thewidth = width / tsy;
tagwidth = (int) Math.round(thewidth);
} else {
tagheight = (int) Math.round(theheight);
}
/* 用于AffineTransform.getScaleInstance返回表示缩放变换的变换。 */
double sx = (double) tagwidth / src.getWidth();
double sy = (double) tagheight / src.getHeight();
/*
* TYPE_INT_RGB:表示一个图像,具有打包成整数像素的 8 位 RGB 颜色分量
* TYPE_INT_ARGB:比RGB多了透明度的alpha值
*/
BufferedImage tag = new BufferedImage(tagwidth, tagheight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = tag.createGraphics();
/* 为呈现算法设置单个首选项的值 */
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
/* 呈现 RenderedImage,在绘制前进行从图像空间到用户空间的转换 */
g.drawRenderedImage(src, AffineTransform.getScaleInstance(sx, sy));
/* 释放此图形的上下文并释放它所使用的所有系统资源 */
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试图片缩放
public static void main(String[] args) throws IOException {
// ImageUtils.resize("D:\\photo\\005.jpg", 480, 320,true);
Calendar calendar = Calendar.getInstance();
long startTime = calendar.getTimeInMillis();
// 测试图片转换
ImageUtils.scale("D:\\photo\\12.jpg", "D:\\photo\\12test.jpg", 39, 39);
Calendar calendar1 = Calendar.getInstance();
long endTime = calendar1.getTimeInMillis();
// 测试图片转换的时间
System.out.println("图片转换的时间: " + String.valueOf(endTime - startTime)
+ "毫秒");
}
}