JAI压缩TIF图片问题

Amos_Object 2018-10-15 08:28:08
公司需要对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的图片改后缀即可
...全文
556 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2018-10-16
  • 打赏
  • 举报
回复
你的import呢 用什么组件做的? 用code方式发代码
Amos_Object 2018-10-16
  • 打赏
  • 举报
回复



这是引用的包


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import com.sun.media.imageio.plugins.tiff.TIFFTag;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFField;


//import cn.adtex.common.kwm.CommonErrCodes;
import cn.adtex.common.component.exception.ErrCodes;
import cn.adtex.common.component.exception.TxnException;


用的是JAI 这个是需要客户将图片提交到服务器 之后利用JAI对TIFF进行压缩合并

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧