如何压缩tiff图像

时间:2013-08-08 09:30:39

标签: java

我目前正在运行时将tiff图像存储在BufferredImage中,并在hmtl的图像src标记中显示该图像。

我的tiff图片大小约为60kb,目前在网络浏览器中加载大约需要1秒钟。

有没有办法压缩tiff图像或BufferedImage,以便在浏览器中加载图像的时间更快。

以下是我在BufferredImage中保存tiff图像的代码。

public BufferedImage savetiff(File srcFilePath) throws IOException {
            FileSeekableStream stream = new FileSeekableStream(srcFilePath);
            TIFFDecodeParam decodeParam = new TIFFDecodeParam();

            decodeParam.setDecodePaletteAsShorts(true);
            ParameterBlock params = new ParameterBlock();

            params.add(stream);
            RenderedOp image1 = JAI.create("tiff", params);
            BufferedImage img = image1.getAsBufferedImage();

            return img;
    }

将tiff转换为图像的代码但在此代码中我必须将jpeg图像保存在磁盘中,然后我必须再次读取它。如果有任何方法可以将tiff转换为JPEG并保存在BufferedImage中。

public boolean tiff2JPG(File srcFilePath, File destFilePath) {
            boolean status = false;
            SeekableStream s = null;
            RenderedImage op = null;
            ImageDecoder dec = null;
            TIFFDecodeParam param = null;
            FileOutputStream fos = null;
            JPEGEncodeParam jpgparam = null;
            ImageEncoder en = null;
            try {
                    s = new FileSeekableStream(srcFilePath);
                    dec = ImageCodec.createImageDecoder("tiff", s, param);
                    op = dec.decodeAsRenderedImage(0);
                    fos = new FileOutputStream(destFilePath);
                    jpgparam = new JPEGEncodeParam();

                    jpgparam.setQuality(67);
                    en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
                    en.encode(op);
                    status = true;
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (Exception e) {
                    e.printStackTrace();
            } finally {
                    try {
                            fos.flush();
                            fos.close();
                    } catch (IOException io) {
                            // TODO Auto-generated catch block
                            io.printStackTrace();
                    } catch (Exception e) {
                            e.printStackTrace();
                    }

            }
return status;

请建议。

的问候,
Dinesh Pise

0 个答案:

没有答案