Using JProgressBar while converting image to byte array

独自空忆成欢 提交于 2019-11-28 14:47:45
MadProgrammer

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!