java如何修改图片大小
2个回答
展开全部
截取点代码片断,你自己看着改
修改图片大小用getScaledInstance方法
BufferedImage bimg = null;
Image img = null;
int width, height;
try {
bimg = ImageIO.read(u);
if(bimg.getWidth() > bimg.getHeight())
{
width = 40;
height = bimg.getHeight() * width / bimg.getWidth();
}
else
{
height = 40;
width = bimg.getWidth() * height / bimg.getHeight();
}
img = bimg.getScaledInstance(width, height, Image.SCALE_DEFAULT);
pre_image = bimg.getScaledInstance(width * 10, height * 10, Image.SCALE_DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
展开全部
就用自带的swing都可以把
/**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static void resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
BufferedImage prevImage = ImageIO.read(is);
double width = prevImage.getWidth();
double height = prevImage.getHeight();
double percent = size/width;
int newWidth = (int)(width * percent);
int newHeight = (int)(height * percent);
BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
ImageIO.write(image, format, os);
os.flush();
is.close();
os.close();
}
/**
* 改变图片的大小到宽为size,然后高随着宽等比例变化
* @param is 上传的图片的输入流
* @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
* @param size 新图片的宽
* @param format 新图片的格式
* @throws IOException
*/
public static void resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
BufferedImage prevImage = ImageIO.read(is);
double width = prevImage.getWidth();
double height = prevImage.getHeight();
double percent = size/width;
int newWidth = (int)(width * percent);
int newHeight = (int)(height * percent);
BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
ImageIO.write(image, format, os);
os.flush();
is.close();
os.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询