Using JProgressBar while converting image to byte array

前端 未结 1 843
再見小時候
再見小時候 2020-12-12 08:23

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())         


        
相关标签:
1条回答
  • 2020-12-12 08:46

    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

    0 讨论(0)
提交回复
热议问题