java代碼jpg轉tif,需要三個jar包,pom下載不了,貼在代碼最後面有下載資源,maven打包方式也有了

 /**
     *  圖片轉tif格式
     * @param bytes
     * @return
     */
    public static byte[] jpg2Tif(byte[] bytes) {

        ByteArrayOutputStream byteArrayOutputStream = null;
        ByteArraySeekableStream stream = null;
        try {
            //輸入流轉換爲stream
            stream = new ByteArraySeekableStream(bytes);
            PlanarImage in = JAI.create("stream", stream);
            //tif輸出
            byteArrayOutputStream = new ByteArrayOutputStream();
            // 設置dpi爲300
            TIFFEncodeParam param = new TIFFEncodeParam();
            param.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
            TIFFField[] extras = new TIFFField[2];
            extras[0] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 300, 1}, {0, 0}});
//            extras[0] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) dpi, 1}, {0, 0}});
            extras[1] = new TIFFField(283, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 300, 1}, {0, 0}});
            param.setExtraFields(extras);
            TIFFImageEncoder enc = new TIFFImageEncoder(byteArrayOutputStream, param);
            try {
                enc.encode(in);
                byteArrayOutputStream.flush();
            } catch (Exception e) {
                logger.error("{}", e);
                throw new RuntimeException(e);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 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;
    }
mvn install:install-file -DgroupId=javax.localmedia   -DartifactId=jai_core -Dversion=1.1.3  -Dfile=D:\download\image2Tif-master\jai_core-1.1.3.jar    -Dpackaging=jar
mvn install:install-file -DgroupId=javax.localmedia   -DartifactId=jai_imageio -Dversion=1.1  -Dfile=D:\download\image2Tif-master\jai_imageio.jar    -Dpackaging=jar
mvn install:install-file -DgroupId=javax.localmedia   -DartifactId=jai-codec -Dversion=1.1.3  -Dfile=D:\download\image2Tif-master\jai-codec-1.1.3.jar    -Dpackaging=jar

這裏打包需要注意修改你的maven倉庫中setting的本地倉庫位置,修改如下,上面文件位置和下面的打包位置都得修改,這樣纔可以打包到自己的本地倉庫,然後再pom中加入如下代碼即可


  <localRepository>D:/maven</localRepository>
        <dependency>
            <groupId>javax.localmedia</groupId>
            <artifactId>jai_core</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>javax.localmedia</groupId>
            <artifactId>jai_imageio</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.localmedia</groupId>
            <artifactId>jai-codec</artifactId>
            <version>1.1.3</version>
        </dependency>

這裏是byte轉byte,一般項目都不緩存本地文件的,所以這個使用範圍比較廣,代碼和jar包下載地址點擊下載jar包和代碼,如果遇到tif格式比較大,請看我的這篇博客Java JPG轉TIF文件過大的解決方案

 

 

打賞二維碼,多謝支持

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章