java怎么剪裁图片

vaivxuanzi 2010-04-30 10:18:13
类似csdn上传头像之后剪裁的功能,
比如上传一张100xp×100px的图片 怎么将其剪裁成 50xp×50px的图片
...全文
198 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycj80217 2012-06-20
  • 打赏
  • 举报
回复
看看JMAGIC
onechen 2011-10-02
  • 打赏
  • 举报
回复
额· 0 0 我也想问这个问题啊
henryiloveyou 2010-05-12
  • 打赏
  • 举报
回复
哪位仁兄传个源码撒
vaivxuanzi 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hq1305018 的回复:]

缩放用的.
Java code

public static void scale(String srcFile, int destWidth, int destHeight, String destFile) throws IOException {
BufferedImage src = ImageIO.read(new File(srcFile));
……
[/Quote]
不过谢谢还是要说滴~~~
vaivxuanzi 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hq1305018 的回复:]

缩放用的.
Java code

public static void scale(String srcFile, int destWidth, int destHeight, String destFile) throws IOException {
BufferedImage src = ImageIO.read(new File(srcFile));
……
[/Quote]

哦买噶。。。。金山词霸都翻译不过来了
hq1305018 2010-04-30
  • 打赏
  • 举报
回复
缩放用的.

public static void scale(String srcFile, int destWidth, int destHeight, String destFile) throws IOException {
BufferedImage src = ImageIO.read(new File(srcFile));
BufferedImage dest = new BufferedImage(destWidth,destHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = dest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(
(double)destWidth/src.getWidth(),
(double)destHeight/src.getHeight());
g.drawRenderedImage(src,at);
ImageIO.write(dest,"JPG",new File(destFile));
}

public static File scale(File srcFile, int destWidth) throws IOException {
BufferedImage src = ImageIO.read(srcFile);
if (src.getWidth() <= destWidth) {
return srcFile;
}
int destHeight = (int)(src.getHeight() * ((double)destWidth/src.getWidth()));
BufferedImage dest = new BufferedImage(destWidth,destHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = dest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(
(double)destWidth/src.getWidth(),
(double)destHeight/src.getHeight());
g.drawRenderedImage(src,at);

String fileSufix = srcFile.getName().substring(srcFile.getName().lastIndexOf(".") + 1);
File destFile = new File(srcFile.getParent(), UUID.randomUUID().toString() + "." + fileSufix);
ImageIO.write(dest,fileSufix,destFile);
return destFile;
}

public static void main(String[] args) {
// if (args.length == 2) {
// try {
// scale(new File(args[0]),Integer.parseInt(args[1]));
// } catch (Exception e) {
// System.out.println(e);
// }
// } else
// System.out.println("paramater error!");
try {
scale(new File("e:/temp/1.gif"), 100);
scale(new File("e:/temp/2.png"), 100);
scale(new File("e:/temp/3.jpg"), 100);
scale(new File("e:/temp/4.jpeg"), 100);
} catch (Exception ex) {
System.out.println(ex);
}

}
vaivxuanzi 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 buqi001 的回复:]

是剪切?还是缩放啊?
这里有个剪切的,不知道是不是LZ想要的
public class ImageCut {
// ===源图片路径名称如:c:\1.jpg
private String srcpath;

// ===剪切图片存放路径名称.如:c:\2.jpg
private String subpath;

// ===剪切点x坐标
……
[/Quote]

缩放又是怎么样的呢??
huaye 2010-04-30
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
buqi001 2010-04-30
  • 打赏
  • 举报
回复
是剪切?还是缩放啊?
这里有个剪切的,不知道是不是LZ想要的
public class ImageCut {
// ===源图片路径名称如:c:\1.jpg
private String srcpath;

// ===剪切图片存放路径名称.如:c:\2.jpg
private String subpath;

// ===剪切点x坐标
private int x;

private int y;

// ===剪切点宽度
private int width;

private int height;


//

/**
* @param srcImage 需要裁减的源图片
* @param outImage 裁减后的图片
* @param x 图片x开始像素(注意是像素为单位)
* @param y 图片y开始像素
* @param width 图片像素宽度
* @param height 图片像素高度
*/
public ImageCut(String srcImage, String outImage, int x, int y, int width,
int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
setSrcpath(srcImage);
setSubpath(outImage);
try {
cut();
} catch (Exception e) {
e.printStackTrace();
}
}
public void cutImag(){

Image croppedImage;
// Image sourceImage = new Image();
ImageFilter cropFilter;

cropFilter =new CropImageFilter(25,30,75,75);//四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API
// croppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));
//如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。
//FilteredImageSource是一个ImageProducer对象。


}
/**
* 对图片裁剪,并把裁剪完蛋新图片保存 。
*
*/
private void cut() throws IOException {
FileInputStream is = null;
ImageInputStream iis = null;

try {
// 读取图片文件
is = new FileInputStream(srcpath);


/*******************************************************************
* 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 声称能够解码指定格式。
* 参数:formatName - 包含非正式格式名称 . (例如 "jpeg" 或 "gif")等 。
*/
Iterator<ImageReader> it = ImageIO
.getImageReadersByFormatName("gif");
ImageReader reader = it.next();
// 获取图片流
iis = ImageIO.createImageInputStream(is);

/*******************************************************************
* <p>
* iis:读取源.true:只向前搜索
* </p>
* .将它标记为 ‘只向前搜索’。 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader
* 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
*/
reader.setInput(iis, true);

/**
* <p>
* 描述如何对流进行解码的类
* <p>
* .用于指定如何在输入时从 Java Image I/O 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 将从其
* ImageReader 实现的 getDefaultReadParam 方法中返回 ImageReadParam 的实例。
*/
ImageReadParam param = reader.getDefaultReadParam();

/*******************************************************************
* 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
* 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。
*/
Rectangle rect = new Rectangle(x, y, width, height);

// 提供一个 BufferedImage,将其用作解码像素数据的目标。
param.setSourceRegion(rect);

/*******************************************************************
* 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 它作为一个完整的
* BufferedImage 返回。
*/
BufferedImage bi = reader.read(0, param);

// 保存新图片
ImageIO.write(bi, "gif", new File(subpath));
}

finally {
if (is != null)
is.close();
if (iis != null)
iis.close();
}

}

qazwsxhai 2010-04-30
  • 打赏
  • 举报
回复
不懂,帮顶.
vaivxuanzi 2010-04-30
  • 打赏
  • 举报
回复
//自己顶

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧