public static void tif_2_jpg(String path)throws Exception{ FileSeekableStream ss = new FileSeekableStream(path); TIFFDecodeParam param0 = null; TIFFEncodeParam param = new TIFFEncodeParam(); JPEGEncodeParam param1 = new JPEGEncodeParam(); ImageDecoder dec = ImageCodec.createImageDecoder("tiff",ss,param0); int count = dec.getNumPages(); param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); param.setLittleEndian(false); System.out.println("这个文件有"+ count+"个图片"); File file = null; for (int i = 0; i < count; i++) { RenderedImage page = dec.decodeAsRenderedImage(i); file = new File(path.replace(path.substring(path.lastIndexOf("."),path.length()),i+"_tiff_.jpg")); System.out.println("SAVEING "+file.getAbsoluteFile()); ParameterBlock pb = new ParameterBlock(); pb.addSource(page); pb.add(file.toString()); pb.add("JPEG"); pb.add(param1); JAI.create("filestore",pb); } } 操作老版本的tiff
public static void tiffToJPEGByImageIO(String tiff) { ImageInputStream input; try { input = ImageIO.createImageInputStream(new File(tiff));//以图片输入流形式读取到tif // Get the reader while (ImageIO.getImageReaders(input).hasNext()){ ImageReader reader = ImageIO.getImageReaders(input).next();//获得image阅读器,阅读对象为tif文件转换的流 String path,tiffName; path = tiff.substring(0, tiff.lastIndexOf(".")); tiffName = tiff.substring(tiff.lastIndexOf("\\"),tiff.lastIndexOf(".")); try { reader.setInput(input); // Read page 2 of the TIFF file int count = reader.getNumImages(true);//tif文件页数 //System.out.println(count); for(int i = 0; i < count; i++){ BufferedImage image = reader.read(i, null);//取得第i页 File f = new File(path+"_"+i+".jpg"); ImageIO.write(image, "JPEG", f);//保存图片 } }catch (IOException e){ e.printStackTrace(); } finally { System.out.println(tiff+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); reader.dispose(); input.close(); } } }catch (IOException e) { e.printStackTrace(); } } 操作pdf
public static void PDF2ImagPDFbox(String filePath)throws Exception { PDDocument pdf = null; pdf = PDDocument.load(new File(filePath)); PDFRenderer pdfRenderer = new PDFRenderer(pdf); PDPageTree pageTree = pdf.getPages(); int pageCounter = 0; for (int i = 0; i < pageTree.getCount(); i++) { float width = pageTree.get(i).getCropBox().getWidth(); float scale = 1.0f; if (width > 720) { scale = 720 / width; } BufferedImage bim = pdfRenderer.renderImage(i, scale, ImageType.RGB); ImageIOUtil.writeImage(bim, filePath.replace(filePath.substring(filePath.lastIndexOf("."), filePath.length()), i + ".jpg"), 300); System.out.println("!!!!!!!!!!!!!!!!"); } }