Java Code Examples for javax.imageio.ImageWriteParam#canWriteCompressed()

The following examples show how to use javax.imageio.ImageWriteParam#canWriteCompressed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageFileWriters.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getWriterParam(ImageAttributes attributes,
        ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            } else {
                String[] compressionTypes = param.getCompressionTypes();
                if (compressionTypes != null) {
                    param.setCompressionType(compressionTypes[0]);
                }
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 2
Source File: ImageGifFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes,
        ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 3
Source File: ImagePnmFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 4
Source File: ImagePngFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 5
Source File: ImageJpgFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 6
Source File: ImageTiffFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            } else {
                param.setCompressionType("LZW");
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 7
Source File: ScreenCapture.java    From logbook-kai with MIT License 6 votes vote down vote up
/**
 * BufferedImageをJPEG形式にエンコードします
 *
 * @param image BufferedImage
 * @return JPEG形式の画像
 * @throws IOException 入出力例外
 */
static byte[] encodeJpeg(BufferedImage image) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (ImageOutputStream ios = ImageIO.createImageOutputStream(out)) {
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        try {
            ImageWriteParam iwp = writer.getDefaultWriteParam();
            if (iwp.canWriteCompressed()) {
                iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                iwp.setCompressionQuality(QUALITY);
            }
            writer.setOutput(ios);
            writer.write(null, new IIOImage(image, null, null), iwp);
        } finally {
            writer.dispose();
        }
    }
    return out.toByteArray();
}
 
Example 8
Source File: ImageHelper.java    From olat with Apache License 2.0 6 votes vote down vote up
private static ImageWriteParam getOptimizedImageWriteParam(ImageWriter writer, Size scaledSize) {
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    try {
        if (iwp.canWriteCompressed()) {
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            int maxSize = Math.max(scaledSize.getWidth(), scaledSize.getHeight());
            if (maxSize <= 50) {
                iwp.setCompressionQuality(0.95f);
            } else if (maxSize <= 100) {
                iwp.setCompressionQuality(0.90f);
            } else if (maxSize <= 200) {
                iwp.setCompressionQuality(0.85f);
            } else if (maxSize <= 500) {
                iwp.setCompressionQuality(0.80f);
            } else {
                iwp.setCompressionQuality(0.75f);
            }
        }
    } catch (Exception e) {
        // bmp can be compressed but don't allow it!!!
        return writer.getDefaultWriteParam();
    }
    return iwp;
}
 
Example 9
Source File: ImageHelper.java    From olat with Apache License 2.0 6 votes vote down vote up
private static ImageWriteParam getOptimizedImageWriteParam(ImageWriter writer, Size scaledSize) {
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    try {
        if (iwp.canWriteCompressed()) {
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            int maxSize = Math.max(scaledSize.getWidth(), scaledSize.getHeight());
            if (maxSize <= 50) {
                iwp.setCompressionQuality(0.95f);
            } else if (maxSize <= 100) {
                iwp.setCompressionQuality(0.90f);
            } else if (maxSize <= 200) {
                iwp.setCompressionQuality(0.85f);
            } else if (maxSize <= 500) {
                iwp.setCompressionQuality(0.80f);
            } else {
                iwp.setCompressionQuality(0.75f);
            }
        }
    } catch (Exception e) {
        // bmp can be compressed but don't allow it!!!
        return writer.getDefaultWriteParam();
    }
    return iwp;
}
 
Example 10
Source File: Img.java    From java-tool with Apache License 2.0 6 votes vote down vote up
public void writeTo(OutputStream os, String mimeType) {
    ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType(mimeType)).next();
    dropAlphaChannelIfJPEG(writer);
    ImageWriteParam params = writer.getDefaultWriteParam();

    if (!Float.isNaN(compressionQuality) && params.canWriteCompressed()) {
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionType(params.getCompressionTypes()[0]);
        params.setCompressionQuality(compressionQuality);
    }

    ImageOutputStream ios = os(os);
    writer.setOutput(ios);
    IIOImage image = new IIOImage(get(), null, null);
    try {
        writer.write(null, image, params);
    } catch (IOException e) {
        throw ioException(e);
    }
    IO.flush(ios);
    writer.dispose();
}
 
Example 11
Source File: ImageListener.java    From logbook-kai with MIT License 5 votes vote down vote up
/**
 * 画像をjpeg形式で再圧縮します。
 *
 * @param in InputStream
 * @return InputStream
 */
private InputStream compressImage(InputStream in) {
    try {
        BufferedImage image = ImageIO.read(in);

        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage canvas = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics gc = canvas.createGraphics();
        gc.setColor(Color.WHITE);
        gc.fillRect(0, 0, width, height);
        gc.drawImage(image, 0, 0, null);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (ImageOutputStream ios = ImageIO.createImageOutputStream(out)) {
            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
            try {

                ImageWriteParam iwp = writer.getDefaultWriteParam();
                if (iwp.canWriteCompressed()) {
                    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                    iwp.setCompressionQuality(0.8f);
                }
                writer.setOutput(ios);
                writer.write(null, new IIOImage(canvas, null, null), iwp);
            } finally {
                writer.dispose();
            }
        }
        return new ByteArrayInputStream(out.toByteArray());
    } catch (Exception e) {
        return null;
    }
}
 
Example 12
Source File: ImageWriterCompressionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    Locale.setDefault(Locale.US);

    final BufferedImage image
        = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = image.createGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                             RenderingHints.VALUE_RENDER_QUALITY);
        g2d.scale(2.0, 2.0);

        g2d.setColor(Color.red);
        g2d.draw(new Rectangle2D.Float(10, 10, 100, 100));
        g2d.setColor(Color.blue);
        g2d.fill(new Rectangle2D.Float(12, 12, 98, 98));
        g2d.setColor(Color.green);
        g2d.setFont(new Font(Font.SERIF, Font.BOLD, 14));

        for (int i = 0; i < 15; i++) {
            g2d.drawString("Testing Compression ...", 20, 20 + i * 16);
        }

        final String[] fileSuffixes = ImageIO.getWriterFileSuffixes();

        final Set<String> testedWriterClasses = new HashSet<String>();

        for (String suffix : fileSuffixes) {

            if (!IGNORE_FILE_SUFFIXES.contains(suffix)) {
                final Iterator<ImageWriter> itWriters
                    = ImageIO.getImageWritersBySuffix(suffix);

                final ImageWriter writer;
                final ImageWriteParam writerParams;

                if (itWriters.hasNext()) {
                    writer = itWriters.next();

                    if (testedWriterClasses.add(writer.getClass().getName())) {
                        writerParams = writer.getDefaultWriteParam();

                        if (writerParams.canWriteCompressed()) {
                            testCompression(image, writer, writerParams, suffix);
                        }
                    }
                } else {
                    throw new RuntimeException("Unable to get writer !");
                }
            }
        }
    } catch (IOException ioe) {
        throw new RuntimeException("IO failure", ioe);
    }
    finally {
        g2d.dispose();
    }
}
 
Example 13
Source File: CaptureDialog.java    From logbook with MIT License 4 votes vote down vote up
@Override
public void run() {
    try {
        // 時刻からファイル名を作成
        Date now = Calendar.getInstance().getTime();

        // 範囲をキャプチャする
        BufferedImage image = AwtUtils.getCapture(this.rectangle);

        String dir = AppConfig.get().getCapturePath();
        String name = this.fileNameFormat.format(now) + "." + AppConfig.get().getImageFormat();
        Path path = Paths.get(dir, name);

        File file = path.toFile();

        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (!(file.canWrite()))
                throw new IOException("File '" + file + "' cannot be written to");
        } else {
            File parent = file.getParentFile();
            if ((parent != null) &&
                    (!(parent.mkdirs())) && (!(parent.isDirectory()))) {
                throw new IOException("Directory '" + parent + "' could not be created");
            }
        }

        if (image != null) {

            try (ImageOutputStream ios = ImageIO.createImageOutputStream(file)) {
                ImageWriter writer = ImageIO.getImageWritersByFormatName(AppConfig.get().getImageFormat())
                        .next();
                try {
                    ImageWriteParam iwp = writer.getDefaultWriteParam();
                    if (iwp.canWriteCompressed()) {
                        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                        iwp.setCompressionQuality(QUALITY);
                    }
                    writer.setOutput(ios);

                    if (this.trimRect == null) {
                        this.trimRect = AwtUtils.getTrimSize(image);
                    }

                    writer.write(null, new IIOImage(AwtUtils.trim(image, this.trimRect), null, null), iwp);
                } finally {
                    writer.dispose();
                }
            }
        }
    } catch (Exception e) {
        LoggerHolder.LOG.warn("キャプチャ中に例外が発生しました", e);
    }
}