Java JPG转TIF文件过大的解决方案(单张解决方案,多张可看以下参考链接)

    /**
     * 图片转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

打赏二维码,多谢支持

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值