JAVA实现图片处理缩略图-三种开源实现方式

代码实现如下:

第一个开源: Thumbnailator

long begin = System.currentTimeMillis();

try {

Thumbnails.of(new File("E:\\test\\photos\\亲密敌人.jpg"))

.size(300, 200)

//.rotate(90)

.outputQuality(0.8f)

.toFile(new File("E:\\000.jpg"));

} catch (IOException e) {

e.printStackTrace();

}

long end = System.currentTimeMillis();

System.out.println(end - begin);

 

第二个开源: java-image-scaling

long begin2 = System.currentTimeMillis();

File fileOne = new File("E:\\test\\photos\\阿凡达.jpg");    

File hd_ad = new File("E:\\test\\data\\高清页面-广告图片.jpg"); 

try {

BufferedImage bufferedImage11 = new ThumpnailRescaleOp(1104,540).doFilter(ImageIO.read(fileOne),null,1104, 540);

ImageIO.write(bufferedImage11, "jpg", hd_ad);

} catch (IOException e) {

e.printStackTrace();

}

long end2 = System.currentTimeMillis();

System.out.println(end2-begin2);

 

第三个开源: EasyImage

long begin3 = System.currentTimeMillis();

Image image2  = new Image("E:\\test\\photos\\亲密敌人.jpg");

image2.resize(290, 400);

image2.saveAs("e:\\222.jpg");

long end3 = System.currentTimeMillis();

System.out.println(end3 - begin3);

 

//融合两张图片

Image image3  = new Image("E:\\test\\photos\\亲密敌人.jpg");

image3.combineWithPicture("E:\\test\\photos\\泰坦尼克号.jpg");

image3.saveAs("E:\\333.jpg");

 

//强调图像的某个部分:

Image image4  = new Image("E:\\test\\photos\\亲密敌人.jpg");

image4.emphasize(250, 200, 2300, 500);

image4.saveAs("E:\\444.jpg");

 

java-image-scaling图片失真问题解决方案:

原因:图片本身的问题(ICC丢失或不全)

 

public static BufferedImage toBufferedImage(File img) throws IOException {

 

        Image image = Toolkit.getDefaultToolkit().getImage(img.getPath());

 

        BufferedImage bimage = null;

 

        if (image instanceof BufferedImage) {

 

            return (BufferedImage) image;

 

        }

 

        image = new ImageIcon(image).getImage();

 

        int width = image.getWidth(null);

 

        int height = image.getHeight(null);

 

        if (width < 0 || height < 0) {

 

            /*Map<String,Integer> map = getImageSize(img);

 

            width = map.get(IMAGE_WIDTH);

 

            height = map.get(IMAGE_HEIGHT);*/

 

            return null;

 

        }

 

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

 

        try {

 

            int transparency = Transparency.OPAQUE;

 

            GraphicsDevice gs = ge.getDefaultScreenDevice();

 

            GraphicsConfiguration gc = gs.getDefaultConfiguration();

 

            bimage = gc.createCompatibleImage(width, height, transparency);

 

        } catch (HeadlessException e) {

 

            e.printStackTrace();

 

        } catch (Exception e) {

 

            e.printStackTrace();

 

        }

 

 

 

        if (bimage == null) {

 

            int type = BufferedImage.TYPE_INT_RGB;

 

            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);

 

        }

 

        Graphics g = bimage.createGraphics();

 

        g.drawImage(image, 0, 0, null);

 

        g.dispose();

 

        return bimage;

 

    }

调用:

File fileOne = new File("E:\\test\\photos\\test.jpg");

BufferedImage bufferedImageTmp2  = null;

try {

bufferedImageTmp2  = toBufferedImage(fileOne);

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

BufferedImage bufferedImage1 = new ThumpnailRescaleOp(470,300).doFilter(bufferedImageTmp2,null,470, 300);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java提供的ImageIO类和缩放算法来实现图片缩略图。具体步骤如下: 1. 读取原始图片,使用ImageIO.read()方法。 2. 创建一个BufferedImage对象,使用原始图片的宽度和高度作为参数。 3. 获取Graphics2D对象,使用BufferedImage对象的createGraphics()方法。 4. 设置Graphics2D对象的渲染质量和抗锯齿。 5. 使用Graphics2D对象的drawImage()方法将原始图片绘制到BufferedImage对象中。 6. 使用ImageIO.write()方法将BufferedImage对象保存为缩略图。 7. 关闭Graphics2D对象和输入输出流。 示例代码如下: ``` import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageUtils { public static void createThumbnail(String sourceImagePath, String targetImagePath, int targetWidth, int targetHeight) throws Exception { BufferedImage sourceImage = ImageIO.read(new File(sourceImagePath)); BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = targetImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(sourceImage, , , targetWidth, targetHeight, null); ImageIO.write(targetImage, "JPEG", new File(targetImagePath)); graphics2D.dispose(); } } ``` 调用示例: ``` ImageUtils.createThumbnail("source.jpg", "target.jpg", 100, 100); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值