Skip to main content

IIOReadProgressListener don't catch a reading process

4 replies [Last post]
nika
Offline
Joined: 2006-08-22
Points: 0

Hello,

I have following code for show progress of image reading.

import javax.imageio.event.*;
import javax.imageio.*;

class ImageReadProgressListener implements IIOReadProgressListener {
// implementation of listeners methods
}

class ProgressReader {
private ImageReadProgressListener listener = new ImageReadProgressListener();
//........
private void read(File f, String ext) {
if(f != null) {
Iterator iter = ImageIO.getImageReadersByFormatName(ext);
while(iter.hasNext()) {
ImageReader imgReader = (ImageReader)iter.next();
imgReader.addIIOReadProgressListener(listener);
}

BufferedImage img = ImageIO.read(f);
}
}
//........
}

In work my custom listener don't catch anything :-(
I run code for JPG, PNG and TIFF files.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

Code looks OK but it would be more useful if you were to post a complete
self-contained test case which can be compiled and run without ANY editing,
i.e., no modules aside from the JVM+JAI+JAI-ImageIO, no hard-coded paths,
preferably no package names.

Brian

On Tue, 22 Aug 2006, jai-imageio@javadesktop.org wrote:

> Hello,
>
> I have following code for show progress of image reading.
>
> import javax.imageio.event.*;
> import javax.imageio.*;
>
> class ImageReadProgressListener implements IIOReadProgressListener {
> // implementation of listeners methods
> }
>
> class ProgressReader {
> private ImageReadProgressListener listener = new ImageReadProgressListener();
> //........
> private void read(File f, String ext) {
> if(f != null) {
> Iterator iter = ImageIO.getImageReadersByFormatName(ext);
> while(iter.hasNext()) {
> ImageReader imgReader = (ImageReader)iter.next();
> imgReader.addIIOReadProgressListener(listener);
> }
>
> BufferedImage img = ImageIO.read(f);
> }
> }
> //........
> }
>
> In work my custom listener don't catch anything :-(
> I run code for JPG, PNG and TIFF files.
> [Message sent by forum member 'nika' (nika)]
>
> http://forums.java.net/jive/thread.jspa?messageID=145537
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

nika
Offline
Joined: 2006-08-22
Points: 0

Hello Brain

Here are approximately application code:

[code]
import java.io.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.imageio.event.*;

class ProgressReader {
private File file;
private String extension;
private IIOReadProgressAdapter listener;

class IIOReadProgressAdapter implements IIOReadProgressListener {
public void imageComplete(ImageReader source) {}

public void imageProgress(ImageReader source, float percentageDone) {
System.err.println("%");
}

public void imageStarted(ImageReader source, int imageIndex) {}

public void readAborted(ImageReader source) {}

public void sequenceComplete(ImageReader source) {}

public void sequenceStarted(ImageReader source, int minIndex) {}

public void thumbnailComplete(ImageReader source) {}

public void thumbnailProgress(ImageReader source, float percentageDone) {
System.err.print("#");
}

public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) {}
}

public ProgressReader(String path) {
if(path == null || path.equals("")) {
return;
}
File f = new File(path);

String ext = f.getName().substring(f.getName().lastIndexOf('.')+1);
if(isGrapthics(ext)) {
file = f;
extension = ext;
}

this.listener = new IIOReadProgressAdapter();

if(file != null && extension != null)
readFile(file, extension);
}

private boolean isGrapthics(String ext) {
return (ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg")
|| ext.equalsIgnoreCase("tif") || ext.equalsIgnoreCase("tiff")
|| ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("gif"));
}

private void readFile(File f, String ext) {
Iterator iter = ImageIO.getImageReadersByFormatName(ext);
while(iter.hasNext()) {
ImageReader imgReader = (ImageReader)iter.next();
imgReader.addIIOReadProgressListener(listener);
}

BufferedImage img = null;

try {
img = ImageIO.read(f);
} catch(IOException e) {
System.err.println("EXCEPTION: ImageIO.read(...) with path " + f.getName());
}

System.out.println("Image was " + (img != null ? "readed" : "unreaded"));
}

public static void main(String[] args) {
if(args.length <= 0) {
System.err.println("WARNING: Please add file path");
return;
}

String pathToFile = args[0];
new ProgressReader(pathToFile);
}
}

[/code]

I changed code to another read(int) method, and it's work fine.

[code]
private void read(File f, String ext) {
if(f != null) {
Iterator iter = ImageIO.getImageReadersByFormatName(ext);
ImageReader imgReader = null;
while(iter.hasNext()) {
imgReader = (ImageReader)iter.next();
imgReader.addIIOReadProgressListener(listener);
}
if (imgReader != null) {
try {
imgReader.setInput(new FileImageInputStream(f));
BufferedImage img = imgReader.read(0);
} catch (IOException e) {
System.err.println("EXCEPTION");
}
}
}
}
[/code]

Brian Burkhalter

Hello Nika,

So is there still a problem or not?

Brian

On Tue, 22 Aug 2006, jai-imageio@javadesktop.org wrote:

> Hello Brain
>
> Here are approximately application code:
>
> [code]
> import java.io.*;
> import java.util.*;
> import java.awt.image.*;
> import javax.imageio.*;
> import javax.imageio.event.*;
>
> class ProgressReader {
> private File file;
> private String extension;
> private IIOReadProgressAdapter listener;
>
> class IIOReadProgressAdapter implements IIOReadProgressListener {
> public void imageComplete(ImageReader source) {}
>
> public void imageProgress(ImageReader source, float percentageDone) {
> System.err.println("%");
> }
>
> public void imageStarted(ImageReader source, int imageIndex) {}
>
> public void readAborted(ImageReader source) {}
>
> public void sequenceComplete(ImageReader source) {}
>
> public void sequenceStarted(ImageReader source, int minIndex) {}
>
> public void thumbnailComplete(ImageReader source) {}
>
> public void thumbnailProgress(ImageReader source, float percentageDone) {
> System.err.print("#");
> }
>
> public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) {}
> }
>
> public ProgressReader(String path) {
> if(path == null || path.equals("")) {
> return;
> }
> File f = new File(path);
>
> String ext = f.getName().substring(f.getName().lastIndexOf('.')+1);
> if(isGrapthics(ext)) {
> file = f;
> extension = ext;
> }
>
> this.listener = new IIOReadProgressAdapter();
>
> if(file != null && extension != null)
> readFile(file, extension);
> }
>
> private boolean isGrapthics(String ext) {
> return (ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg")
> || ext.equalsIgnoreCase("tif") || ext.equalsIgnoreCase("tiff")
> || ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("gif"));
> }
>
> private void readFile(File f, String ext) {
> Iterator iter = ImageIO.getImageReadersByFormatName(ext);
> while(iter.hasNext()) {
> ImageReader imgReader = (ImageReader)iter.next();
> imgReader.addIIOReadProgressListener(listener);
> }
>
> BufferedImage img = null;
>
> try {
> img = ImageIO.read(f);
> } catch(IOException e) {
> System.err.println("EXCEPTION: ImageIO.read(...) with path " + f.getName());
> }
>
> System.out.println("Image was " + (img != null ? "readed" : "unreaded"));
> }
>
> public static void main(String[] args) {
> if(args.length <= 0) {
> System.err.println("WARNING: Please add file path");
> return;
> }
>
> String pathToFile = args[0];
> new ProgressReader(pathToFile);
> }
> }
>
> [/code]
>
> I changed code to another read(int) method, and it's work fine.
>
> [code]
> private void read(File f, String ext) {
> if(f != null) {
> Iterator iter = ImageIO.getImageReadersByFormatName(ext);
> ImageReader imgReader = null;
> while(iter.hasNext()) {
> imgReader = (ImageReader)iter.next();
> imgReader.addIIOReadProgressListener(listener);
> }
> if (imgReader != null) {
> try {
> imgReader.setInput(new FileImageInputStream(f));
> BufferedImage img = imgReader.read(0);
> } catch (IOException e) {
> System.err.println("EXCEPTION");
> }
> }
> }
> }
> [/code]
> [Message sent by forum member 'nika' (nika)]
>
> http://forums.java.net/jive/thread.jspa?messageID=146025
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Thorsten Heit

Hi,

> I have following code for show progress of image reading.
>
> import javax.imageio.event.*;
> import javax.imageio.*;
>
> class ImageReadProgressListener implements IIOReadProgressListener {
> // implementation of listeners methods
> }
>
> class ProgressReader {
> private ImageReadProgressListener listener = new
> ImageReadProgressListener();
> //........
> private void read(File f, String ext) {
> if(f != null) {
> Iterator iter = ImageIO.getImageReadersByFormatName(ext);
> while(iter.hasNext()) {
> ImageReader imgReader = (ImageReader)iter.next();
> imgReader.addIIOReadProgressListener(listener);
> }

I haven't tried it, but why don't use use imgReader.read() to
read your image? Example:

boolean ok = false;
BufferedImage[] images = null;
while (!ok && iter.hasNext()) {
ImageReader imgReader = (ImageReader)iter.next();
imgReader.addIIOReadProgressListener(listener);
int numImages = imgReader.getNumImages(false);
if (numImages > 0) {
images = new BufferedImage[numImages];
try {
for (int i = 0; i images[i] = imgReader.read(i);
}
ok = true;
} catch (IOException ioex) {
// add your preferred error handler here
}
}
}

Regards

Thorsten
[PGP.sig]