高分问图片缩放问题

amdgaming 2009-12-15 08:22:27
想求点代码 如何将一个 图片,jpg或者png,同比 缩小 或放大 ,注意不要模糊,或者变形,同比 是最好了

谢谢了哈

...全文
162 17 打赏 收藏 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
kf156 2009-12-16
  • 打赏
  • 举报
回复
我只有J2ME的图片缩放,嘿嘿
一头头 2009-12-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 amdgaming 的回复:]
引用 9 楼 coldanimal 的回复:
get to go to work, the rest of part is your own busniess ahahahaha

兄弟 转出来的图片 很模糊啊。难道 一定要  要用你第三方 类库啊
[/Quote]

图片的压缩 里面最关键的还是算法哇 傻X算法肯定出来模糊的图片啊

要用好的算法 那只能third-party用起来了哇 这个是没办法的额
nielei317 2009-12-16
  • 打赏
  • 举报
回复
兄弟,如果你说图片扩大不失真的话,难。缩小但是没什么问题。
amdgaming 2009-12-16
  • 打赏
  • 举报
回复
呵呵,用了 java 的 自带的缩小 效果不错,放大 不能望啊
amdgaming 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 coldanimal 的回复:]
get to go to work, the rest of part is your own busniess ahahahaha
[/Quote]
兄弟 转出来的图片 很模糊啊。难道 一定要 要用你第三方 类库啊
ahhh90h 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 amdgaming 的回复:]
引用 7 楼 amdgaming 的回复:
楼上的程序不错啊,我想要的效果是 转化后的文件 不要在本地生产文件,直接生成字节数组就可以

然后我通过ImagIcon(byte[] byteData,String description)来创建一个图片


如何转啊???
[/Quote]
realreachard 2009-12-15
  • 打赏
  • 举报
回复
mark
amdgaming 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 amdgaming 的回复:]
楼上的程序不错啊,我想要的效果是 转化后的文件 不要在本地生产文件,直接生成字节数组就可以

然后我通过ImagIcon(byte[] byteData,String description)来创建一个图片

[/Quote]
如何转啊???
一头头 2009-12-15
  • 打赏
  • 举报
回复
get to go to work, the rest of part is your own busniess ahahahaha
一头头 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 amdgaming 的回复:]
引用 5 楼 coldanimal 的回复:
come on dude, you are not freshman........

but i dont think this function work when using PNG input.

你英文不错啊, 不过话说反了吧。哈哈
[/Quote]

很普通哇 只是现在试着多用用罢了 png可以用哦? 那不错哇 我本来想是不能用的 因为png有点动画的意思
amdgaming 2009-12-15
  • 打赏
  • 举报
回复
楼上的程序不错啊,我想要的效果是 转化后的文件 不要在本地生产文件,直接生成字节数组就可以

然后我通过ImagIcon(byte[] byteData,String description)来创建一个图片
amdgaming 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 coldanimal 的回复:]
come on dude, you are not freshman........

but i dont think this function work when using PNG input.
[/Quote]
你英文不错啊, 不过话说反了吧。哈哈
一头头 2009-12-15
  • 打赏
  • 举报
回复
come on dude, you are not freshman........

but i dont think this function work when using PNG input.
amdgaming 2009-12-15
  • 打赏
  • 举报
回复
不错,如果是png呢?这个方法也可以把

[Quote=引用 3 楼 coldanimal 的回复:]
引用 2 楼 amdgaming 的回复:
这个 要引入 第三方jar包,楼上的 兄弟

能不能不要第三方jar包的 谢谢了



I dont try it, hope it works.

Java codepublicclass ImageUtils {private Image srcImage=null;private File srcFile=null;private File destFile=null;private String fileSuffix=null;privateint imageWidth=0;privateint imageHeight=0;public ImageUtils(String fileName)throws IOException {this(new File(fileName));
}public ImageUtils(File fileName)throws IOException {
File _file= fileName;
_file.setReadOnly();this.srcFile= _file;this.fileSuffix= _file.getName().substring(
(_file.getName().indexOf(".")+1),
(_file.getName().length()));this.destFile=new File(this.srcFile.getPath().substring(0,
(this.srcFile.getPath().lastIndexOf(".")))+ PIC_TEMPORARY+"."+this.fileSuffix);
srcImage= javax.imageio.ImageIO.read(_file);//得到图片的原始大小, 以便按比例压缩。 imageWidth= srcImage.getWidth(null);
imageHeight= srcImage.getHeight(null);
System.out.println("width:"+ imageWidth);
System.out.println("height:"+ imageHeight);
}/**
* 强制压缩/放大图片到固定的大小
*@param w int 新宽度
*@param h int 新高度
*@throws IOException*/publicvoid resize(int w,int h)throws IOException {//得到合适的压缩大小,按比例。if ( imageWidth>= imageHeight)
{
w= w;
h= (int)Math.round((imageHeight* w*1.0/ imageWidth));
}else
{
h= h;
w= (int)Math.round((imageWidth* h*1.0/ imageHeight));
}//构建图片对象 BufferedImage _image=new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);//绘制缩小后的图 _image.getGraphics().drawImage(srcImage,0,0, w, h,null);//输出到文件流 FileOutputStream out=new FileOutputStream(destFile);
JPEGImageEncoder encoder= JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image);
out.flush();
out.close();

}
}
[/Quote]
一头头 2009-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 amdgaming 的回复:]
这个 要引入 第三方jar包,楼上的 兄弟

能不能不要第三方jar包的 谢谢了

[/Quote]

I dont try it, hope it works.

public  class ImageUtils {   
private Image srcImage = null;
private File srcFile = null;
private File destFile = null;
private String fileSuffix = null;

private int imageWidth = 0;
private int imageHeight = 0;

public ImageUtils(String fileName) throws IOException {
this(new File(fileName));
}

public ImageUtils(File fileName) throws IOException {
File _file = fileName;
_file.setReadOnly();
this.srcFile = _file;
this.fileSuffix = _file.getName().substring(
(_file.getName().indexOf(".") + 1),
(_file.getName().length()));
this.destFile = new File(this.srcFile.getPath().substring(0,
(this.srcFile.getPath().lastIndexOf(".")))
+ PIC_TEMPORARY + "." + this.fileSuffix);
srcImage = javax.imageio.ImageIO.read(_file);
//得到图片的原始大小, 以便按比例压缩。
imageWidth = srcImage.getWidth(null);
imageHeight = srcImage.getHeight(null);
System.out.println("width: " + imageWidth);
System.out.println("height: " + imageHeight);
}

/**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
//得到合适的压缩大小,按比例。
if ( imageWidth >= imageHeight)
{
w = w;
h = (int)Math.round((imageHeight * w * 1.0 / imageWidth));
}
else
{
h = h;
w = (int)Math.round((imageWidth * h * 1.0 / imageHeight));
}

//构建图片对象
BufferedImage _image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图
_image.getGraphics().drawImage(srcImage, 0, 0, w, h, null);
//输出到文件流
FileOutputStream out = new FileOutputStream(destFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image);
out.flush();
out.close();

}
}
amdgaming 2009-12-15
  • 打赏
  • 举报
回复
这个 要引入 第三方jar包,楼上的 兄弟

能不能不要第三方jar包的 谢谢了
一头头 2009-12-15
  • 打赏
  • 举报
回复
I recommend image4j which could be downloaded from http://image4j.sourceforge.net/
after you get the jar file, you can implement your feature by net.sf.image4j.util.ImageUtil.scaleImage

From API,



scaleImage
public static java.awt.image.BufferedImage scaleImage(java.awt.image.BufferedImage src,int width,int height)Creates a scaled copy of the source image.

Parameters:
src - source image to be scaled
width - the width for the new scaled image in pixels
height - the height for the new scaled image in pixels
Returns:
a copy of the source image scaled to width x height pixels.

62,620

社区成员

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

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