如何缩放任意格式图片?

stanely 2005-07-06 02:52:30
在网上找到如下代码,但不是很明白,请各位帮助:

import java.awt.image.*;
import java.awt.*;
import java.net.*;

public class app extends java.applet.Applet {
Image source;
Image resizedImage;

public void init() {
MediaTracker media = new MediaTracker(this);
// java how-to image for example
source = getImage(getDocumentBase(),"jht.gif");
media.addImage(source,0);
try {
media.waitForID(0);
// scale down, half the original size
ImageFilter replicate =
new ReplicateScaleFilter
(source.getWidth(this)/2, source.getHeight(this)/2);
ImageProducer prod =
new FilteredImageSource(source.getSource(),replicate);
resizedImage = createImage(prod);
media.addImage(resizedImage,1);
media.waitForID(1);
}
catch(InterruptedException e) {}
}

public void paint(Graphics g) {
g.drawImage(source, 10,10,this);
g.drawImage(resizedImage,10, 80,this);
}
}

1。如果巴"jht.gif"改成任何格式的图片文件名称,还能奏效吗?
2。怎么最后没有存储缩放后图片的代码?
3。对于jpg图片,如此缩放后压缩质量怎样保留或改变?
4。如果哪位能提供功能更强大的任意(或者多种)图片格式缩放代码,小弟将不胜感激!!!!

...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreamno 2005-07-06
  • 打赏
  • 举报
回复
public static boolean renderImage(OutputStream output,
InputStream input) {

try {
// イメージの読み込み | read image use JAI(SeekableSteam ,RenderedOp)
SeekableStream ss = SeekableStream.wrapInputStream(input, true);
PlanarImage src = JAI.create("stream", ss);

// サムネールの作成 // | Construct Thumbnail

// 画像サイズの取得 | get the size of image
float srcWidth = Float.parseFloat(src.getProperty("image_width").
toString());
float srcHeight = Float.parseFloat(src.getProperty("image_height").
toString());

// 倍率の割り出し | fit diameter
float srcScale = srcWidth / srcHeight;

float tagWidth = 0, tagHeight = 0;
if (srcScale < IMAGE_SCALE) {
tagHeight = srcHeight > IMAGE_HEIGHT ? IMAGE_HEIGHT : srcHeight;
tagWidth = tagHeight * srcScale;

}
else {
tagWidth = srcWidth > IMAGE_WIDTH ? IMAGE_WIDTH : srcWidth;
tagHeight = tagWidth / srcScale;
}

float xScale = (float) tagWidth / srcWidth;
float yScale = (float) tagHeight / srcHeight;

// サイズを変更 | change the size use java.awt.image.render.ParameterBlock
ParameterBlock pb = new ParameterBlock();
pb.addSource(src);
//--modify--
//---new---
pb.add(xScale); //width scale
pb.add(yScale); //height scale
//---end---
pb.add(0.0F);
pb.add(0.0F);
src = JAI.create("scale", pb, null);

// エンコード |encode
RenderedOp small = JAI.create("encode", src, output, "PNG", null);
output.flush();
return true;
}
catch (Exception e) {
return false;
}

}
stanely 2005-07-06
  • 打赏
  • 举报
回复
请问JAI是什么?哪个包?谢谢!
stanely 2005-07-06
  • 打赏
  • 举报
回复
险些忘了一点:

5。单纯代码缩放的图片效果不会很好,谁有alpha或者其他模糊算法能够使所放出来图片看上去更自然,请不吝赐教,小弟另有分相赠!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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