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

打赏二维码,多谢支持

猜你喜欢

转载自blog.csdn.net/gonepoo/article/details/104853791
今日推荐