图片放大 java,用Java放大和缩小图像

I think the question is quite self-explainitory, I want to implement a simple zoom function using a JSlider like in Windows Live Photo Gallery for instance.

I've had a quick look online but all the code I've tried to use appears to have errors in when I copy it into eclipse. I don't really want to use a third party library either as the application may be sold under a company name. Plus, I'm begginning to realise that there may be some safety precations required in order to prevent errors, but I do not know what these will be.

So, if anybody can provide me with some Java code to zoom in and out of images it would be grately appreciated.

Thanks in advance

PS I plan to use the Image as an ImageIcon inside of a JLabel which will be added to a JScrollPane

解决方案

You can easily achieve this by using scale transforms on the original image.

Assuming your the current image width newImageWidth, and the current image height newImageHeight, and the current zoom level zoomLevel, you can do the following:

int newImageWidth = imageWidth * zoomLevel;

int newImageHeight = imageHeight * zoomLevel;

BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, imageType);

Graphics2D g = resizedImage.createGraphics();

g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);

g.dispose();

Now, replace the original image, originalImage, in your display area by resizedImage.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值