Java JPEG fixImageIOJpegBug(BufferedImage image)

Here you can find the source of fixImageIOJpegBug(BufferedImage image)

Description

fix Image IO Jpeg Bug

License

Open Source License

Declaration

private static BufferedImage fixImageIOJpegBug(BufferedImage image) 

Method Source Code

//package com.java2s;
/*/* w  w  w . j a v a2  s .  c om*/
 *  Copyright (C) 2010-2016 JPEXS, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

public class Main {
    private static BufferedImage fixImageIOJpegBug(BufferedImage image) {
        int type = image.getType();
        if (type != BufferedImage.TYPE_INT_RGB) {
            // convert to RGB without alpha channel
            int width = image.getWidth();
            int height = image.getHeight();
            BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            int[] pixels2 = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
            int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
            for (int i = 0; i < pixels.length; i++) {
                pixels2[i] = pixels[i] & 0xffffff;
            }

            return newImage;
        }

        return image;
    }
}

Related

  1. jpeg2BufferedImage(final byte[] jpegImage, final boolean bw)
  2. jpegFromImage(BufferedImage image)
  3. png2jpeg(File pngImage, File jpegFile)