[java]tif, tiff파일을 png파일로 변환하기
INTRO
tif파일 :
TIFF (Tagged Image File Format)는 앨더스(Aldus)사와 마이크로소프트사가 공동 개발한 이미지 저장 포맷으로
보통 팩스로 받은 파일이 tif형식으로 이루어진 경우가 많습니다. 여러장의 팩스를 받았을 경우 하나의 파일 안에 여러 이미지가 있습니다.(참고자료출처 :http://kr.aving.net/news/view.php?articleId=1940)
팩스문서, pdf파일과 같은 한개의 파일 안에 여러개의 이미지가 있는 경우 웹에서는 바로 로드하지 못합니다.
이럴 때 서버에서 tif파일을 읽어 png 파일로 분할하여 사용하기 편한 형식으로 바꿔 html에서 제어하면 됩니다.
CONTENTS
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
74
75
|
import java.io.File;
import java.io.FileNotFoundException;
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.TIFFEncodeParam;
public static createPng(path, name){
String rename = name.replace(".tif", "");
String pngFile = "";
try {
FileSeekableStream fss = new FileSeekableStream(path+name);
ImageDecoder imgdec = ImageCodec.createImageDecoder("tiff", fss, null);
int count = imgdec .getNumPages();
int startPage = 0;
String strCount = Integer.toString(count);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setLittleEndian(false);
String filePath = "D:/testFolder/"+rename+"_"; // 파일이 생성되는 경로, 파일을 읽어오는 경로
for (int i = 0; i < count; i++) {
RenderedImage page = dec.decodeAsRenderedImage(i);
BufferedImage image = PlanarImage.wrapRenderedImage(page).getAsBufferedImage();
File outputfile = new File(filePath+count +"_"+(i+1)+".png");
pngFile += filePath+count +"_"+(i+1)+".png&";
ImageIO.write(image, "png", outputfile);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
|
cs |
OUTRO
참고자료 및 자료출처 : https://github.com/sjh010/tifViewer/blob/master/src/main/java/com/mobileleader/tifleader/util/ConvertUtil.java
'Programming > Java' 카테고리의 다른 글
[java]zip파일로 묶어 저장하기 (0) | 2022.09.05 |
---|---|
[JAVA]문자열 치환,삭제 replace(); (0) | 2022.09.05 |