(转)http://wangminger.iteye.com/blog/1134106
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74package org.tif; import java.awt.image.RenderedImage; import java.awt.image.renderable.ParameterBlock; import java.io.File; import java.io.IOException; import javax.media.jai.JAI; import com.sun.media.jai.codec.FileSeekableStream; import com.sun.media.jai.codec.ImageCodec; import com.sun.media.jai.codec.ImageDecoder; import com.sun.media.jai.codec.JPEGEncodeParam; import com.sun.media.jai.codec.TIFFDecodeParam; import com.sun.media.jai.codec.TIFFEncodeParam; /** * 读取tif图片的一张.tif有几页 * */ public class ReadTifPage { private static final String path = System.getProperty("user.dir") + "\src\org\tif\tif_images.tif"; public static void main(String[] args) throws Exception { //readTifPage(); readTifImages(); } /** * 单纯读tif文件的页数 */ private static void readTifPage() { File file = null; Long tifPage; file = new File(path); if (file.isFile()) { FileSeekableStream fs = null; TIFFDecodeParam tifParam = new TIFFDecodeParam(); try { fs = new FileSeekableStream(file); ImageDecoder dec = ImageCodec.createImageDecoder("tiff", fs, tifParam); tifPage = new Long(dec.getNumPages()); System.out.println("这张图片有多少页码: " + tifPage); } catch (IOException e) { e.printStackTrace(); } } } /** * 分页读取 * * @throws IOException */ private static void readTifImages() throws IOException { 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); // Intel System.out.println("这个Tif文件 有" + count + "十张图片"); for (int i = 0; i <count; i++) { RenderedImage page = dec.decodeAsRenderedImage(i); File f = new File("./img_" + i + ".jpg"); System.out.println("Saving " + f.getCanonicalPath()); ParameterBlock pb = new ParameterBlock(); pb.addSource(page); pb.add(f.toString()); pb.add("JPEG"); pb.add(param1); JAI.create("filestore", pb); //JAI.create("filestore", page, "./Img_new" + i +".jpg", "JPEG", param1); } } }
最后
以上就是迅速期待最近收集整理的关于多页单个tif文件转换为多个jpg文件的全部内容,更多相关多页单个tif文件转换为多个jpg文件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复