I was just wondering how I would resize an image in java?
This is for an assignment where I have to locate an image and then save it as a .png file that is half the resolution as the original.
This is my code so far;
enter code here
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class saveimage {
public static void main(String[] args) // IOException
{
String sourceLocation;
sourceLocation = (args[0]);
int width = 963;
int height = 640;
int halfwidth = width / 2;
int halfheight = height / 2;
BufferedImage image1 = null;
BufferedImage imagehalf = null;
File readfile = null;
try {
readfile = new File(sourceLocation);
image1 = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
image1 = ImageIO.read(readfile);
imagehalf = new BufferedImage(halfwidth, halfheight,
BufferedImage.TYPE_INT_ARGB);
imagehalf = ImageIO.read(readfile);
System.out.println("reading complete");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
try {
readfile = new File("LOCATION OF FILE");
ImageIO.write(image1, "png", readfile);
System.out.println("Writing complete");
} catch (IOException fail1) {
System.out.println("Error:" + fail1);
}
try {
readfile = new File "LOCATION OF OUTPUT");
ImageIO.write(imagehalf, "png", readfile);
System.out.println("writing half is complete");
} catch (IOException fail2) {
System.out.println("Error:" + fail2);
}
}
}
As you can see I have just halved the integer values at the start as I thought it would have just halved the output size but it didn't...is there anything i am doing wrong?
The next part of the assignment is that i need to tile the image but i am just doing one step at a time :)
Thanks in advance
AFAIK imagehalf = ImageIO.read(readfile); would just read the image file and create a BufferedImage of the original size. What you're basically doing is create a fresh BufferedImage and then replace it with one read from the file, which can't work.
What you'd have to do instead: read the original image, create a half sized BufferedImage and draw the original image to the half sized one.
Use the getGraphics() method and call drawImage(...) with the necessary parameters on the returned Graphics object.
Note that you could use BufferedImage#getScaledInstance(...) but you might want to start using the Graphics object to be prepared for your future assignments.
Hey feel free to use this code i posted below:
public ImageIcon resizeImage(ImageIcon imageIcon, int width, int height, boolean max)
{
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
int width1 = newimg.getWidth(null);
if ((max && width1 > width) || (!max && width1 < width))
newimg = image.getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(newimg);
}
I actually don't know what the boolean max does i found it on the internet somewhere.
You need to get a graphics context for the resizedImage and then draw the original image into it with the dimensions you want. Depending on how it looks you may want to look at Java's RenderingHints as well.
BufferedImage imageHalf = new BufferedImage(halfwidth, halfheight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = imageHalf.createGraphics();
g.drawImage(fullImage, 0, 0, halfwidth, halfheight, null);
g.dispose();
Related
I have an grayscale image with dimension 256*256.I am trying to downscale it to 128*128.
I am taking an average of two pixel and writing it to the ouput file.
class Start {
public static void main (String [] args) throws IOException {
File input= new File("E:\\input.raw");
File output= new File("E:\\output.raw");
new Start().resizeImage(input,output,2);
}
public void resizeImage(File input, File output, int downScaleFactor) throws IOException {
byte[] fileContent= Files.readAllBytes(input.toPath());
FileOutputStream stream= new FileOutputStream(output);
int i=0;
int j=1;
int result=0;
for(;i<fileContent.length;i++)
{
if(j>1){
// skip the records.
j--;
continue;
}
else {
result = fileContent[i];
for (; j < downScaleFactor; j++) {
result = ((result + fileContent[i + j]) / 2);
}
j++;
stream.write( fileContent[i]);
}
}
stream.close();
}
}
Above code run successfully , I can see the size of output file size is decreased but when I try to convert
output file (raw file) to jpg online (https://www.iloveimg.com/convert-to-jpg/raw-to-jpg) it is giving me an error saying that file is corrupt.
I have converted input file from same online tool it is working perfectly. Something is wrong with my code which is creating corrupt file.
How can I correct it ?
P.S I can not use any library which directly downscale an image .
Your code is not handling image resizing.
See how-to-resize-images-in-java.
Which, i am copying a simple version here:
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageResizer {
public static void resize(String inputImagePath,
String outputImagePath, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth,
scaledHeight, inputImage.getType());
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath
.lastIndexOf(".") + 1);
// writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
}
public static void resize(String inputImagePath,
String outputImagePath, double percent) throws IOException {
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
int scaledWidth = (int) (inputImage.getWidth() * percent);
int scaledHeight = (int) (inputImage.getHeight() * percent);
resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
}
public static void main(String[] args) {
String inputImagePath = "resources/snoopy.jpg";
String outputImagePath1 = "target/Puppy_Fixed.jpg";
String outputImagePath2 = "target/Puppy_Smaller.jpg";
String outputImagePath3 = "target/Puppy_Bigger.jpg";
try {
// resize to a fixed width (not proportional)
int scaledWidth = 1024;
int scaledHeight = 768;
ImageResizer.resize(inputImagePath, outputImagePath1, scaledWidth, scaledHeight);
// resize smaller by 50%
double percent = 0.5;
ImageResizer.resize(inputImagePath, outputImagePath2, percent);
// resize bigger by 50%
percent = 1.5;
ImageResizer.resize(inputImagePath, outputImagePath3, percent);
} catch (IOException ex) {
System.out.println("Error resizing the image.");
ex.printStackTrace();
}
}
}
I'm capturing images from a scanner device with java. The input format ist PGM or TIFF. I have to show up live results in the user interface. Actually I'm using ImageJ to read the source input stream as tiff, because ImageJ can also handle incomplete streams. After that the ImagePlus object is converted into a BufferedImage and finally into a JavaFX Image.
ImagePlus imagePlus = new Opener().openTiff(inputStream, "");
BufferedImage bufferedImage = imagePlus.getBufferedImage();
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
This is very slow. I need a faster way to create the JavaFX Image from the PGM or TIFF stream. It seems that JavaFX has actually no support for this formats and I don't found a usefull library.
Any idea?
Edit #1
I've decided to optimze the image capturing in two steps. At first I need a better state control when updating the image in the ui. This is actually done and works fine. Now update requests are dropped, when the conversion thread is busy. The second step is to use a self implemented pnm reader (based on the suggested implementation) and update the image in my model incrementally... until the scan process is complete. This should reduce the required recources when loading an image from the device. I need to change some parts of my architecture to make this happen.
Thanks # all for comments.
btw: java 8 lambdas are great :)
Edit #2
My plan doesn't work, because of JavaFX's thread test :(
Currently I have a WritableImage in my backend wich should be filled step by step with data. This image instance is set to an ObjectProperty that is finally bound to the ImageView. Since the WritableImage is connected to the ImageView it's impossible to fill it with data by using a PixelWriter. This causes an exception.
java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-2-thread-1
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:210) ~[jfxrt.jar:na]
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393) ~[jfxrt.jar:na]
at javafx.scene.Scene.addToDirtyList(Scene.java:529) ~[jfxrt.jar:na]
at javafx.scene.Node.addToSceneDirtyList(Node.java:417) ~[jfxrt.jar:na]
at javafx.scene.Node.impl_markDirty(Node.java:408) ~[jfxrt.jar:na]
at javafx.scene.Node.transformedBoundsChanged(Node.java:3789) ~[jfxrt.jar:na]
at javafx.scene.Node.impl_geomChanged(Node.java:3753) ~[jfxrt.jar:na]
at javafx.scene.image.ImageView.access$700(ImageView.java:141) ~[jfxrt.jar:na]
at javafx.scene.image.ImageView$3.invalidated(ImageView.java:285) ~[jfxrt.jar:na]
at javafx.beans.WeakInvalidationListener.invalidated(WeakInvalidationListener.java:83) ~[jfxrt.jar:na]
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135) ~[jfxrt.jar:na]
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80) ~[jfxrt.jar:na]
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74) ~[jfxrt.jar:na]
at javafx.scene.image.Image$ObjectPropertyImpl.fireValueChangedEvent(Image.java:568) ~[jfxrt.jar:na]
at javafx.scene.image.Image.pixelsDirty(Image.java:542) ~[jfxrt.jar:na]
at javafx.scene.image.WritableImage$2.setArgb(WritableImage.java:170) ~[jfxrt.jar:na]
at javafx.scene.image.WritableImage$2.setColor(WritableImage.java:179) ~[jfxrt.jar:na]
My workaround is to create a copy of the image, but I don't like this solution. Maybe it's possible to prevent the automatic change notification and do this manually?
As an experiment, and to learn some JavaFX, I decided to see for myself how hard it would be to implement what I suggested in the comment above... :-)
The PGM reading is adapted from my PNM ImageIO plugin, and it seems to work okay. Read times is reported to be around 70-90 ms for my 640x480 sample images (feel free to send me some more samples if you have!).
An uncompressed TIFF should be readable in roughly the same time, although the TIFF IFD structure is more complex to parse than the very simple PGM header. TIFF compression will add some decompression overhead, depending on compression type and settings.
import java.io.DataInputStream;
import java.io.IOException;
import javax.imageio.IIOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PGMTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws IOException {
Label root = new Label();
Image image;
long start = System.currentTimeMillis();
DataInputStream input = new DataInputStream(getClass().getResourceAsStream("/house.l.pgm"));
try {
image = readImage(input);
} finally {
input.close();
}
System.out.printf("Read image (%f x %f) in: %d ms\n", image.getWidth(), image.getHeight(), System.currentTimeMillis() - start);
root.setGraphic(new ImageView(image));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private Image readImage(final DataInputStream input) throws IOException {
// First parse PGM header
PNMHeader header = PNMHeader.parse(input);
WritableImage image = new WritableImage(header.getWidth(), header.getHeight());
PixelWriter pixelWriter = image.getPixelWriter();
int maxSample = header.getMaxSample(); // Needed for normalization
// PixelFormat<ByteBuffer> gray = PixelFormat.createByteIndexedInstance(createGrayColorMap());
byte[] rowBuffer = new byte[header.getWidth()];
for (int y = 0; y < header.getHeight(); y++) {
input.readFully(rowBuffer); // Read one row
// normalize(rowBuffer, maxSample);
// pixelWriter.setPixels(0, y, width, 1, gray, rowBuffer, 0, width); // Gives weird NPE for me...
// As I can't get setPixels to work, we'll set pixels directly
// Performance is probably worse than setPixels, but it seems "ok"-ish
for (int x = 0; x < rowBuffer.length; x++) {
int gray = (rowBuffer[x] & 0xff) * 255 / maxSample; // Normalize [0...255]
pixelWriter.setArgb(x, y, 0xff000000 | gray << 16 | gray << 8 | gray);
}
}
return image;
}
private int[] createGrayColorMap() {
int[] colors = new int[256];
for (int i = 0; i < colors.length; i++) {
colors[i] = 0xff000000 | i << 16 | i << 8 | i;
}
return colors;
}
/**
* Simplified version of my PNMHeader parser
*/
private static class PNMHeader {
public static final int PGM = 'P' << 8 | '5';
private final int width;
private final int height;
private final int maxSample;
private PNMHeader(final int width, final int height, final int maxSample) {
this.width = width;
this.height = height;
this.maxSample = maxSample;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getMaxSample() {
return maxSample;
}
public static PNMHeader parse(final DataInputStream input) throws IOException {
short type = input.readShort();
if (type != PGM) {
throw new IIOException(String.format("Only PGM binay (P5) supported for now: %04x", type));
}
int width = 0;
int height = 0;
int maxSample = 0;
while (width == 0 || height == 0 || maxSample == 0) {
String line = input.readLine(); // For PGM I guess this is ok...
if (line == null) {
throw new IIOException("Unexpeced end of stream");
}
if (line.indexOf('#') >= 0) {
// Skip comment
continue;
}
line = line.trim();
if (!line.isEmpty()) {
// We have tokens...
String[] tokens = line.split("\\s");
for (String token : tokens) {
if (width == 0) {
width = Integer.parseInt(token);
} else if (height == 0) {
height = Integer.parseInt(token);
} else if (maxSample == 0) {
maxSample = Integer.parseInt(token);
} else {
throw new IIOException("Unknown PBM token: " + token);
}
}
}
}
return new PNMHeader(width, height, maxSample);
}
}
}
I should probably add that I wrote, compiled and ran the above code on Java 7, using JavaFX 2.2.
Update: Using a predefined PixelFormat I was able to use PixelWriter.setPixels and thus further reduce read times to 45-60 ms for the same 640x480 sample images. Here's a new version of readImage (the code is otherwise the same):
private Image readImage(final DataInputStream input) throws IOException {
// First parse PGM header
PNMHeader header = PNMHeader.parse(input);
int width = header.getWidth();
int height = header.getHeight();
WritableImage image = new WritableImage(width, height);
PixelWriter pixelWriter = image.getPixelWriter();
int maxSample = header.getMaxSample(); // Needed to normalize
PixelFormat<ByteBuffer> format = PixelFormat.getByteRgbInstance();
byte[] rowBuffer = new byte[width * 3]; // * 3 to hold RGB
for (int y = 0; y < height; y++) {
input.readFully(rowBuffer, 0, width); // Read one row
// Expand gray to RGB triplets
for (int i = width - 1; i > 0; i--) {
byte gray = (byte) ((rowBuffer[i] & 0xff) * 255 / maxSample); // Normalize [0...255];
rowBuffer[i * 3 ] = gray;
rowBuffer[i * 3 + 1] = gray;
rowBuffer[i * 3 + 2] = gray;
}
pixelWriter.setPixels(0, y, width, 1, format, rowBuffer, 0, width * 3);
}
return image;
}
Download jai_imageio.jar and include it in your project.
Code to convert tiff images into fx readable images is below:
String pathToImage = "D:\\ABC.TIF";
ImageInputStream is;
try {
is = ImageIO.createImageInputStream(new File(pathToImage)); //read tiff using imageIO (JAI component)
if (is == null || is.length() == 0) {
System.out.println("Image is null");
}
Iterator<ImageReader> iterator = ImageIO.getImageReaders(is);
if (iterator == null || !iterator.hasNext()) {
throw new IOException("Image file format not supported by ImageIO: " + pathToImage);
}
ImageReader reader = (ImageReader) iterator.next();
reader.setInput(is);
int nbPages = reader.getNumImages(true);
BufferedImage bf = reader.read(0); //1st page of tiff file
BufferedImage bf1 = reader.read(1); //2nd page of tiff file
WritableImage wr = null;
WritableImage wr1 = null;
if (bf != null) {
wr= SwingFXUtils.toFXImage(bf, null); //convert bufferedImage (awt) into Writable Image(fx)
}
if (bf != null) {
wr1= SwingFXUtils.toFXImage(bf1, null); //convert bufferedImage (awt) into Writable Image(fx)
}
img_view1.setImage(wr);
img_view2.setImage(wr1);
} catch (FileNotFoundException ex) {
Logger.getLogger(Image_WindowController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Image_WindowController.class.getName()).log(Level.SEVERE, null, ex);
}
This is my first answer on Stack Overflow. Hope it helps!
So I try to create an image from a byte array, but I can't figure out why the ImageIO.read() method returns a null pointer without any exception.
#Override
public int setParam(byte[] buffer) {
mFlag = buffer[0]; //TODO
mX = Convertor.convert2BytesToInt(buffer[1], buffer[2]);
mY = Convertor.convert2BytesToInt(buffer[3], buffer[4]);
mWidth = Convertor.convert2BytesToInt(buffer[5], buffer[6]);
mHeight = Convertor.convert2BytesToInt(buffer[7], buffer[8]);
mLength = Convertor.convert4BytesToInt(buffer[9], buffer[10], buffer[11], buffer[12]);
byte[] bufferpix = Arrays.copyOfRange(buffer, 13, 13+mLength);
ByteArrayInputStream in = new ByteArrayInputStream(bufferpix);
try {
mImage = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
return 13+mLength;
}
#Override
public void draw(Graphics2D g, ArrayList<Color> palette) {
System.out.print("Draw Image\n");
g.drawImage(mImage, mX, mY, mWidth, mHeight, null);
}
The buffer seems to be okay, it contains data RGBA (1 byte for each, so 4 bytes per pixels).
Do you see any problem with that usage?
Thx
Btw, if you wonder, this buffer has previously been created by the Android class Bitmap.
I wasn't using the right method:
int[] bufferpix = new int[mLength];
for(int i=0; i<mLength;i++){
bufferpix[i] = buffer[i+13];
}
mImage = new BufferedImage(mWidth, mHeight, BufferedImage.TYPE_4BYTE_ABGR_PRE);
mImage.getRaster().setPixels(0, 0, mWidth, mHeight, bufferpix);
This fill my image correctly.
Too bad that setPixels can't take a byte array for parameter, which make the conversion uggly (I haven't look for a better way to copy my bytes array in a int array yet, probably there is one).
I am checking the api of https://pdf-renderer.dev.java.net/
I want to convert the PDF to image at zoom level 100%.
Does any one tried that?
int widthPage = (int)page.getBBox().getWidth();
int heightPage = (int)page.getBBox().getHeight();
if(page.getAspectRatio()<1){
widthPage = (int)(widthPage / page.getAspectRatio() );
heightPage = (int)(heightPage / page.getAspectRatio() );
}
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0, 0, (int) widthPage, (int) heightPage);
// generate the image
Image img = page.getImage(rect.width, rect.height, // width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
// save it as a file
I would suggest iText for PDF viewing. It has been pretty good for me so far.
Just change the Image dimensions. When you zoom the Image all the characters appeared in clear view if the image dimensions are high. Download 04-Request-Headers.pdf file and paste it in C drive, converted files are saved in PDFConvertedFiles folder. Jars Required PDFRenderer-0.9.0
package com.pdfrenderer.examples;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class ConvertAllPDFPagesToImageWithDimenstions {
public static void main(String[] args) {
try {
String sourceDir = "C:/04-Request-Headers.pdf";// PDF file must be placed in DataGet folder
String destinationDir = "C:/PDFConvertedFiles/";//Converted PDF page saved in this folder
File sourceFile = new File(sourceDir);
File destinationFile = new File(destinationDir);
String fileName = sourceFile.getName().replace(".pdf", "");
if (sourceFile.exists()) {
if (!destinationFile.exists()) {
destinationFile.mkdir();
System.out.println("Folder created in: "+ destinationFile.getCanonicalPath());
}
RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
System.out.println("Total Pages: "+ pdf.getNumPages());
int pageNumber = 1;
for (int i = 0; i < pdf.getNumPages(); i++) {
PDFPage page = pdf.getPage(i);
// image dimensions
int width = 1200;
int height = 1400;
// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// width & height, // clip rect, // null for the ImageObserver, // fill background with white, // block until drawing is done
Image image = page.getImage(width, height, rect, null, true, true );
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);
File imageFile = new File( destinationDir + fileName +"_"+ pageNumber +".png" );// change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp
ImageIO.write(bufferedImage, "png", imageFile);
pageNumber++;
System.out.println(imageFile.getName() +" File created in Folder: "+ destinationFile.getCanonicalPath());
}
} else {
System.err.println(sourceFile.getName() +" File not exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
So I have a very small piece of code, which takes a .gif image as input, and then it splits this .gif image into an array of BufferedImage. After that, it stores the images in the array on the disk. When I do this, the output images contain heavy white-pixel noise which isn't visible in the input .gif image.
Example of the input gif:
Example of malformed output image (the 3rd frame in the gif):
The code I am using to split the gif is as follows:
public static void main(String[] args) throws Exception {
splitGif(new File("C:\\test.gif"));
}
public static void splitGif(File file) throws IOException {
ImageReader reader = ImageIO.getImageReadersBySuffix("gif").next(); reader.setInput(ImageIO.createImageInputStream(new FileInputStream(file)), false);
for(int i = 0; i < reader.getNumImages(true); i++) {
BufferedImage image = reader.read(i);
ImageIO.write(image, "PNG", new File(i + ".png"));
}
}
Can anyone help me out?
So the problem was that when reading .gif files into java, then all pixels in a given frame that did not change color compared to their previous frame will be fully transparent. If you want to read a .gif and split it in an array of properly rendered BufferedImages, then you have to fill the transparent pixels of the current frame with the last non-transparent pixel of one of the previous frames.
Code:
public static void splitGif(File file) throws IOException {
ImageReader reader = ImageIO.getImageReadersBySuffix("gif").next();
reader.setInput(ImageIO.createImageInputStream(new FileInputStream(file)), false);
BufferedImage lastImage = reader.read(0);
ImageIO.write(lastImage, "PNG", new File(0 + ".png"));
for (int i = 1; i < reader.getNumImages(true); i++) {
BufferedImage image = makeImageForIndex(reader, i, lastImage);
ImageIO.write(image, "PNG", new File(i + ".png"));
}
}
private static BufferedImage makeImageForIndex(ImageReader reader, int index, BufferedImage lastImage) throws IOException {
BufferedImage image = reader.read(index);
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
if(lastImage != null) {
newImage.getGraphics().drawImage(lastImage, 0, 0, null);
}
newImage.getGraphics().drawImage(image, 0, 0, null);
return newImage;
}
You solved it yourself, but for good order: every next frame is the accumulation of all prior frames filling up the transparent pixels in the current frame.
public static void splitGif(File file) throws IOException {
ImageReader reader = ImageIO.getImageReadersBySuffix("gif").next();
reader.setInput(ImageIO.createImageInputStream(new FileInputStream(file)), false);
BufferedImage outImage = null;
Graphics2D g = null;
for (int i = 0; i < reader.getNumImages(true); i++) {
BufferedImage image = reader.read(i);
if (g == null) {
BufferedImage outImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
g = (Graphics2D) outImage.getGraphics();
}
g.drawImage(lastImage, 0, 0, null);
ImageIO.write(outImage, "PNG", new File(i + ".png"));
}
if (g != null) {
g.dispose();
}
}
getGraphics==createGraphics should be balanced be a dispose as documented.