当前位置:编程学习 > JAVA >>

(Java实现)HTML转JPG,TIFF等图片格式和TIFF图片合并功能解决方案。

 前一小段时间,发现Apache的一个开源工具,可以把PDF转成图片,没有直接从HTML转图片的jar包,就只能曲线救国了。
忘了介绍了。PDF转图片的包叫做apache pdfBox 右边是:pdfbox的官网 pdfBox官网
下面直接贴代码了:
[java] 
public void convertToTiff(String pdfFilePath, String tiffFileName) 
            throws Exception { 
         
         PDDocument doc = PDDocument.load(pdfFilePath); 
            int pageCount = doc.getPageCount();  
            List pages = doc.getDocumentCatalog().getAllPages();  
            List<File> files = new ArrayList<File>(); 
            List<File> deleteFiles = new ArrayList<File>(); 
             
             
            for(int i=0;i<pages.size();i++){ 
                PDPage page = (PDPage)pages.get(i);  
                BufferedImage image = page.convertToImage(BufferedImage.TYPE_BYTE_BINARY, 300); 
                  
             
            Iterator iter = ImageIO.getImageWritersBySuffix("jpg");  
            ImageWriter writer = (ImageWriter)iter.next();  
            File outFile = new File("C:/1"+i+".jpg");  
            FileOutputStream out = new FileOutputStream(outFile);  
             
            ImageOutputStream outImage = ImageIO.createImageOutputStream(out);  
            writer.setOutput(outImage);  
            writer.write(new IIOImage(image,null,null));  
             
             
            this.jpg2tif("C:/1"+i+".jpg", "C:/1"+i+".tif"); 
            files.add(new File("C:/1"+i+".tif"));  
            deleteFiles.add(outFile); 
            deleteFiles.add(new File("C:/1"+i+".tif")); 
             
             
            outImage.close(); 
            out.close(); 
            writer.dispose(); 
        } 
         
        if(files != null){ 
            this.tif2Marge(files, "C:/1.tif"); 
        } 
    } 
[java]
/**
     * 将jpg格式转化为tif格式。
     * @param srcFile  需要装换的源文件
     * @param descFile 装换后的转存文件
     * @throws Exception
     */ 
    public void jpg2tif(String srcFile, String descFile) throws Exception { 
        RenderedOp src = JAI.create("fileload", srcFile); 
        OutputStream os = new FileOutputStream(descFile); 
        TIFFEncodeParam param = new TIFFEncodeParam(); 
        param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);  
        ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", os, param); 
        encoder.encode(src); 
        os.close(); 
    } 
 
    /**
     * 将若干tif文件合同为一个tif文件
     * @param srcFile
     * @param descFile
     * @throws Exception
     */ 
    public void tif2Marge(List<File> srcFile, String descFile) throws Exception { 
        List pages = new ArrayList(srcFile.size() - 1); 
         
        for (int i = 0; i < srcFile.size(); i++) { 
            RenderedOp firstpage = JAI.create("fileload", srcFile.get(0).getCanonicalPath()); 
            if(i != 0 ){ 
                RenderedOp page = JAI.create("fileload", srcFile.get(i).getCanonicalPath()); 
                pages.add(page); 
            } www.zzzyk.com
            OutputStream os = new FileOutputStream(descFile); 
            TIFFEncodeParam param = new TIFFEncodeParam(); 
            param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);  
            param.setExtraImages(pages.iterator()); 
            ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", os, param); 
            encoder.encode(firstpage); 
            firstpage.dispose(); 
    &
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,