我是靠谱客的博主 俊逸天空,最近开发中收集的这篇文章主要介绍java 切图 按比例切图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

java 切图

 

public boolean scaleImageFromUrl(String oldpath, String newpath, int wdith,
int height) {
try {
URL url = null;
url = new URL(oldpath);
URLConnection connection;
connection = url.openConnection();
BufferedImage bi = ImageIO.read(connection.getInputStream());
Image Itemp = bi.getScaledInstance(wdith, height,
BufferedImage.SCALE_SMOOTH);
BufferedImage thumbnail = new BufferedImage(wdith, height,
BufferedImage.TYPE_INT_RGB);
thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
File newimg = new File(newpath);
FileOutputStream out = new FileOutputStream(newimg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(thumbnail);
param.setQuality(1.0f, false);
encoder.encode(thumbnail);
out.close();
bi.flush();
bi = null;
return true;
} catch (IOException e) {
return false;
}
}

 

 

 

public boolean scaleImageFromUrl(String oldpath, String newpath, int wdith) {
try {
int height=367;
URL url = null;
url = new URL(oldpath);
URLConnection connection;
connection = url.openConnection();
BufferedImage bi = ImageIO.read(connection.getInputStream());
if(bi.getWidth()<=550){
wdith=bi.getWidth();
height=bi.getHeight();
}else{
height=(int)Math.floor(wdith*bi.getHeight()/bi.getWidth());
}
Image Itemp = bi.getScaledInstance(wdith, height,
BufferedImage.SCALE_SMOOTH);
BufferedImage thumbnail = new BufferedImage(wdith, height,
BufferedImage.TYPE_INT_RGB);
thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
File newimg = new File(newpath);
FileOutputStream out = new FileOutputStream(newimg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(thumbnail);
param.setQuality(0.8f, false);
encoder.encode(thumbnail);
out.close();
bi.flush();
bi = null;
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

 

最后

以上就是俊逸天空为你收集整理的java 切图 按比例切图的全部内容,希望文章能够帮你解决java 切图 按比例切图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部