问题
The following is a simple code for converting an image to byte array (Which is already shown in this forum):
File imgPath= new File(textFiled_Path.getText());
BufferedImage originalImage = ImageIO.read(imgPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
However, when the image size is big, the program takes too much time to convert. Therefore, I'm thinking of adding a JProgressBar to the GUI that I made to let the user know how much time left. All examples that I've seen are mostly dealing with JProgressBar with loops. I don't know how to start here :( Can I have an idea to start with. In other word, where should I put JProgressBar here.
Thank you in advance.
回答1:
So, you need two things...
First of all, you need some way to monitor the progress of the image loading and writing...This gets a little complex very quickly, as to be able to monitor the progress of the operations, you need to know the actual reader/writer used by ImageIO
....
File file = new File("...");
try (ImageInputStream iis = ImageIO.createImageInputStream(file)) {
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.addIIOReadProgressListener(new IIOReadProgressListener() {
@Override
public void sequenceStarted(ImageReader source, int minIndex) {
}
@Override
public void sequenceComplete(ImageReader source) {
}
@Override
public void imageStarted(ImageReader source, int imageIndex) {
}
@Override
public void imageProgress(ImageReader source, float percentageDone) {
//TODO: Update progress...
}
@Override
public void imageComplete(ImageReader source) {
}
@Override
public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) {
}
@Override
public void thumbnailProgress(ImageReader source, float percentageDone) {
}
@Override
public void thumbnailComplete(ImageReader source) {
}
@Override
public void readAborted(ImageReader source) {
}
});
reader.setInput(iis);
try {
BufferedImage img = reader.read(0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ImageOutputStream ios = ImageIO.createImageOutputStream(baos)) {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("png");
if (writers.hasNext()) {
ImageWriter writer = writers.next();
writer.addIIOWriteProgressListener(new IIOWriteProgressListener() {
@Override
public void imageStarted(ImageWriter source, int imageIndex) {
}
@Override
public void imageProgress(ImageWriter source, float percentageDone) {
// TODO: Update progress
}
@Override
public void imageComplete(ImageWriter source) {
}
@Override
public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {
}
@Override
public void thumbnailProgress(ImageWriter source, float percentageDone) {
}
@Override
public void thumbnailComplete(ImageWriter source) {
}
@Override
public void writeAborted(ImageWriter source) {
}
});
writer.setOutput(ios);
try {
writer.write(img);
} finally {
writer.removeAllIIOWriteProgressListeners();
}
}
}
} finally {
reader.removeAllIIOReadProgressListeners();
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
Okay, now you have that, the next thing you need is some way to execute it outside of the context of the Event Dispatching Thread, so as not to block the UI, so the UI will remain responsive, but you also want a means by which you can easily update the UI without violating the single thread rules (updates to the UI must be done from within the context of the Event Dispatching Thread)
For this, a SwingWorker is well suited.
Essentially, you would use the doInBackground
method to read/write the image, the progress property support could be used update progress indicators and the publish
/process
methods could be used to provide other information about the current operation.
You could even use the done
method to handle the circumstances where you need to update the UI when the doInBackground
method has completed.
For example
来源:https://stackoverflow.com/questions/24815494/using-jprogressbar-while-converting-image-to-byte-array