/**
* 圖片轉tif格式
*
* @param bytes
* @return
*/
public static byte[] jpg2Tif(byte[] bytes) {
//File file = new File("D:\\myname.jpg");
ByteArraySeekableStream stream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
//FileSeekableStream stream = new FileSeekableStream(file.getCanonicalPath());
stream = new ByteArraySeekableStream(bytes);
PlanarImage firstPage = JAI.create("stream", stream);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
TIFFField[] extras = new TIFFField[4];
extras[0] = new TIFFField(262, TIFFField.TIFF_SHORT, 1, (Object) new short[]{6});
extras[1] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});
extras[2] = new TIFFField(283, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});
extras[3] = new TIFFField(258, TIFFField.TIFF_SHORT, 1, (Object) new char[]{8});
param.setExtraFields(extras);
// OutputStream os = new FileOutputStream("D:" + File.separator + "mynametttt.tif");
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", byteArrayOutputStream, param);
enc.encode(firstPage);
byteArrayOutputStream.flush();
//os.close();
System.out.println("over");
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
logger.error("jpg2Tif e.getMessage()" + e.getMessage());
}
}
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
e.printStackTrace();
logger.error("jpg2Tif e.getMessage()" + e.getMessage());
}
}
}
return null;
}
參考鏈接java 多張jpg合成tif後避免tif文件過大的方法,感謝這位博主!!!
以下是可以測試的代碼demo,只需要在D盤放入一張圖片就可以,以下代碼已驗證
public static void jpg2Tif222() {
File file = new File("D:\\myname.jpg");
//ByteArraySeekableStream stream = null;
//ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
FileSeekableStream stream = new FileSeekableStream(file.getCanonicalPath());
//stream = new ByteArraySeekableStream(bytes);
PlanarImage firstPage = JAI.create("stream", stream);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
TIFFField[] extras = new TIFFField[4];
extras[0] = new TIFFField(262, TIFFField.TIFF_SHORT, 1, (Object) new short[]{6});
extras[1] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});
extras[2] = new TIFFField(283, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});
extras[3] = new TIFFField(258, TIFFField.TIFF_SHORT, 1, (Object) new char[]{8});
param.setExtraFields(extras);
OutputStream os = new FileOutputStream("D:" + File.separator + "mynametttt.tif");
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", os, param);
enc.encode(firstPage);
//byteArrayOutputStream.flush();
os.close();
System.out.println("over");
//return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
/*if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
logger.error("jpg2Tif e.getMessage()" + e.getMessage());
}
}
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
e.printStackTrace();
logger.error("jpg2Tif e.getMessage()" + e.getMessage());
}
}*/
}
}
經過測試,3647KB多的圖片轉化成圖片只有645K
打賞二維碼,多謝支持