通过插值方法生成高清缩略图代码

package com;



import java.awt.image.BufferedImage;

import java.io.File;



import javax.imageio.ImageIO;



public class Lanczos {

    private static final double WORK_LOAD_FACTOR = 0.265;



    private static final double LANCZOS_SUPPORT = 3;

    private static final double LANCZOS_WINDOW = 3;

    private static final double LANCZOS_SCALE = 1;

    private static final double LANCZOS_BLUR = 1;



    private static final double EPSILON = 1.0e-6;



    private static class ContributionInfo {

        private double weight;

        private int pixel;

    }



    public static BufferedImage resizeImage(BufferedImage image, double ratio) {

        return resizeImage(image, (int)(image.getWidth() * ratio + 0.5), (int)(image.getHeight() * ratio + 0.5));        

    }



    public static BufferedImage resizeImage(BufferedImage image, double xRatio, double yRatio) {

        return resizeImage(image, (int)(image.getWidth() * xRatio + 0.5), (int)(image.getHeight() * yRatio + 0.5));        

    }



    public static BufferedImage resizeImage(BufferedImage image, int width, int height) {

        double xFactor = width * 1.0 / image.getWidth();

        double yFactor = height * 1.0 / image.getHeight();



        BufferedImage resizeImage = new BufferedImage(width, height, image.getType());

        BufferedImage filterImage = null;



        if (xFactor * yFactor > WORK_LOAD_FACTOR) {

            filterImage = new BufferedImage(width, image.getHeight(), image.getType());

            horizontalFilter(image, filterImage, xFactor);

            verticalFilter(filterImage, resizeImage, yFactor);

        } else {

            filterImage = new BufferedImage(image.getWidth(), height, image.getType());

            verticalFilter(image, filterImage, yFactor);

            horizontalFilter(filterImage, resizeImage, xFactor);

        }

        return resizeImage;

    }



    private static void verticalFilter(BufferedImage image, BufferedImage resizeImage,

            double yFactor) {

        double scale = Math.max(1.0 / yFactor, 1.0);

        double support = scale * LANCZOS_SUPPORT;

        if (support > 16) & 0xFF);

                    green += alpha * ((rgb >>[img]/images/smiles/icon_cool.gif" alt="[/img]
 & 0xFF);

                    blue += alpha * (rgb & 0xFF);

                }

                int rgb = roundToQuantum(red) > 16) & 0xFF);

                    green += alpha * ((rgb >>[img]/images/smiles/icon_cool.gif" alt="[/img]
 & 0xFF);

                    blue += alpha * (rgb & 0xFF);

                }

                int rgb = roundToQuantum(red) = 255)

            return 255;

        return (int) (value + 0.5);

    }



    public static void main(String[] args) throws Exception {

        BufferedImage image = ImageIO.read(new File("C:/Program Files/SuperMap/精细化农业气候区划产品制作系统/Bin/Product/广西壮族自治区/广西荔枝/广西荔枝/20101028/广西壮族自治区_广西荔枝_广西荔枝_20101028.bmp"));

        ImageIO.write(resizeImage(image, 670, 420), "PNG", new File("C:/Program Files/SuperMap/精细化农业气候区划产品制作系统/Bin/Product/广西壮族自治区/广西荔枝/广西荔枝/20101028/广西壮族自治区_广西荔枝_广西荔枝_20101028_testr.bmp"));

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 提供的 ImageIO 类和 BufferedImage 类来生成图片缩略图。下面是一个简单的示例代码: ``` import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ThumbnailGenerator { public static void main(String[] args) throws IOException { // 源图文件路径 File sourceFile = new File("source.jpg"); // 缩略图文件路径 File thumbnailFile = new File("thumbnail.jpg"); // 缩略图大小 int thumbnailWidth = 100; int thumbnailHeight = 100; // 读取源图 BufferedImage sourceImage = ImageIO.read(sourceFile); // 创建缩略图 BufferedImage thumbnailImage = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = thumbnailImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.drawImage(sourceImage, 0, 0, thumbnailWidth, thumbnailHeight, null); g2d.dispose(); // 写入缩略图文件 ImageIO.write(thumbnailImage, "jpg", thumbnailFile); } } ``` 在该示例中,我们首先指定了源图文件路径和缩略图文件路径,然后指定了缩略图的大小。接着,我们使用 ImageIO 类的 read() 方法读取源图,并使用 BufferedImage 类的 createGraphics() 方法创建了一个 Graphics2D 对象,然后使用 Graphics2D 对象的 drawImage() 方法将源图绘制到缩略图上,并指定了插值算法。最后,我们使用 ImageIO 类的 write() 方法缩略图写入文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值