java图片处理

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)
+ "毫秒");
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值