JAI压缩TIF图片问题
公司需要对tif图片进行压缩分页保存,但是出现了当图片过大(1.9M)导致图片压缩不成功,报数组下标越界错误,尝试过改变压缩等级等等都不行希望各位大佬帮忙解决一下!
这是代码:
public class TestJAI {
/**
* @param tzsPath
* @throws TxnException
*/
public static final void processTifFile(String workPath) throws TxnException {
String inWorkPath = workPath;
if(!inWorkPath.endsWith("/")) {
inWorkPath = inWorkPath + "/";
}
File workFile = new File(inWorkPath);
File[] fileList = workFile.listFiles();
ArrayList<String> tifFiles = new ArrayList<String>();
for (File file : fileList) {
if (file.isDirectory()) {
TestJAI.processTifFile(file.getAbsolutePath());
} else {
String fileName = file.getName();
if (fileName.endsWith(".tif") || fileName.endsWith(".tiff")) {
tifFiles.add(fileName);
}
}
}
if (!tifFiles.isEmpty()) {
Collections.sort(tifFiles, new TestJAI.TifFileNameComparator());
PlanarImage firstPage = null;
OutputStream os = null;
ArrayList<PlanarImage> pages = new ArrayList<PlanarImage>();
ArrayList<FileSeekableStream> streams = new ArrayList<FileSeekableStream>();
String tifFile = null;
try {
for (int i = 0; i < tifFiles.size(); i++) {
tifFile = tifFiles.get(i);
FileSeekableStream stream = new FileSeekableStream(
inWorkPath + tifFile);
streams.add(stream);
if (i == 0) {
firstPage = JAI.create("stream", stream);
} else {
PlanarImage page = JAI.create("stream", stream);
pages.add(page);
}
}
/*
If compression is set to any value but COMPRESSION_NONE and
the OutputStream supplied to the ImageEncoder is not a SeekableOutputStream,
then the encoder will use either a temporary file or a memory cache
when compressing the data depending on whether the file system is accessible.
Compression will therefore be more efficient if a SeekableOutputStream is supplied.
如果压缩设置为任意值,除了COMPRESSION_NONE,并且提供给ImageEncoder的OutputStream不是SeekableOutputStream,
那么在压缩数据时,编码器将使用临时文件或内存缓存,这取决于文件系统是否可访问。因此,如果提供SeekableOutputStream,压缩将更加有效。
*/
TIFFEncodeParam param = new TIFFEncodeParam();
//图像数据压缩方案
//param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);//增加压缩属性,防止文件生成过大
param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
//param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_1D);
//param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_2D);
//param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
//param.setCompression(TIFFEncodeParam.COMPRESSION_LZW);
//param.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
/*
* TIFFField(int tag,int type,int length,Object valueoffset )
* tag 属性标签编号
* type 属性值的数据类型
* length(count) 该类型数据的数量
* valueoffset(data) 属性值存放偏移量
*/
TIFFField[] extras = new TIFFField[2];
extras[0] = new TIFFField(256, TIFFTag.TIFF_LONG, 1, 10000L);
extras[1] = new TIFFField(257, TIFFTag.TIFF_LONG, 1, 10000L);
param.setExtraFields(extras);
//压缩等级
//param.setDeflateLevel(9);
//param.setTileSize(6000,6000);
//param.setWriteTiled(true);
//param.setReverseFillOrder(true);
os = new FileOutputStream(inWorkPath + "merge.tiff");
ImageEncoder enc = ImageCodec.createImageEncoder("tiff",os,param);
param.setExtraImages(pages.iterator());
System.out.println("----------");
enc.encode(firstPage);
} catch (FileNotFoundException e) {
throw ErrCodes.FILE_NOTEXIST1.exception(inWorkPath, e);
} catch (IOException e) {
if(tifFile != null) {
throw ErrCodes.FILE_OPEN_ERROR1.exception(inWorkPath + tifFile, e);
}
else {
throw ErrCodes.FILE_OPEN_ERROR1.exception("", e);
}
} catch (java.lang.Error e) {
//压缩属性可能会报java.lang.Error 异常,异常内容如下所示,如果出现,就不进行压缩处理
//TIFFImageEncoder Bilevel encodings are supported for bilevel images only.
try {
TIFFEncodeParam param = new TIFFEncodeParam();
os = new FileOutputStream(inWorkPath + "merge.tiff");
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", os,
param);
param.setExtraImages(pages.iterator());
enc.encode(firstPage);
} catch (FileNotFoundException e1) {
throw ErrCodes.FILE_NOTEXIST1.exception(inWorkPath, e1);
} catch (IOException e1) {
if(tifFile != null) {
throw ErrCodes.FILE_OPEN_ERROR1.exception(inWorkPath + tifFile, e1);
}
else {
throw ErrCodes.FILE_OPEN_ERROR1.exception("", e1);
}
}
} catch (java.lang.Exception e) {
System.out.println("123456");
//throw CommonErrCodes.TZS_MERGE_TIF.exception(inWorkPath, e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
os = null;
}
for (FileSeekableStream stream : streams) {
if (stream != null) {
try {
stream.close();
} catch (Throwable e) {
}
}
}
}
}
if (!tifFiles.isEmpty()) {
for(String tifFile : tifFiles) {
File oriFile = new File(inWorkPath + tifFile);
String newFileName = tifFile.replaceFirst("^0*", "");
oriFile.renameTo(new File(inWorkPath + newFileName));
}
}
}
private static class TifFileNameComparator implements Comparator<String> {
public int compare(String str1, String str2) {
if(str1 == null && str2 == null ) {
return 0;
}
if(str1 == null) {
return -1;
}
if(str2 == null) {
return 1;
}
return str1.compareToIgnoreCase(str2);
}
}
public static void main(String[] args) {
try {
//图片文件路径
processTifFile("E:/GA000188273277/GA000188273277/129H0O5RU06558901538125828184");
} catch (TxnException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
求各位大佬 帮我看一下下 到底是哪里出了错误
tif格式的图片各位可以随意下载一个大于2M的图片改后缀即可