Java——pdf首页转图片制作缩略图

public static final String FILETYPE_JPG = "jpg";
/**
     * 将指定pdf文件的首页转换为指定路径的缩略图
     *@param filepath 原文件路径,例如d:/test.pdf
     *@param imagepath 图片生成路径,例如 d:/test-1.jpg
     *@param zoom     缩略图显示倍数,1表示不缩放,0.3则缩小到30%
     */
public void tranfer(String filepath, String imagepath, float zoom) throws IOException, PDFException, PDFSecurityException ,InterruptedException{
    // ICEpdf document class
    Document document = null;
    float rotation = 0f;
    document = new Document();
    document.setFile(filepath);
    // maxPages = document.getPageTree().getNumberOfPages();
    BufferedImage img = (BufferedImage) document.getPageImage(0, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom);
    Iterator iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);
    ImageWriter writer = (ImageWriter) iter.next();
    File outFile = new File(imagepath);
    FileOutputStream out = new FileOutputStream(outFile);
    ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
    writer.setOutput(outImage);
    writer.write(new IIOImage(img, null, null));
    System.out.println("pdf转换成功!!!");
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用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); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值