Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Perform Function on Newly-Generated Files Within Directory
    primarykey
    data
    text
    <p>I am experiencing problems when running some code that I am developing.</p> <p>It should work as such:</p> <pre><code>For all images within directory (x) Read image Convert to greyscale Save to new directory (y) For all images within new directory (y) Read image Convert to binary Save to new directory (z) End for End for </code></pre> <p>I have 300 images at present and so far all images are successfully converted to greyscale and saved to a new directory. However, the binary conversion is where the problems occur as it appears to not detect any images in the new directory and only appears to work if [image] files already exist within the directory <strong>before</strong> the code is executed.</p> <p>Therefore, the following is what actually happens:</p> <pre><code>All files in directory (x) are read All files in directory (x) are converted to greyscale and saved to new directory (y) All files in directory (y) are read It appears that directory (y) is empty (but, in fact, contains 300 greyscale images) Program ends </code></pre> <p>However, when I run the program a second time, either with the 300 greyscale images or even a single images, the images in <code>directory (y)</code> are successfully converted into binary; it appears to only work if there are pre-existing images in the directory and not whilst the newly-converted-to-greyscale images are being created on-the-fly.</p> <p>The method is called as follows:</p> <pre><code>public static void processFiles(){ processGreyscale(); System.out.println("Greyscale image conversion complete.\n"); processBinary(); System.out.println("Binary image conversion complete.\n"); } </code></pre> <p>I have even tried adding a time-delay between method calls to allow for the system to update itself in order to detect the newly-created [greyscale] images (in <code>directory (y)</code>), but this does not make any difference and the images are only recognised and converted to binary when two conditions are satisfied:</p> <ol> <li>There are images present in <code>directory (y)</code></li> <li>The code is run for a second time <strong>or</strong> if there are any [image] files within the directory <strong>before</strong> the code is first executed.</li> </ol> <p>Is there a way to do this so that the newly-generated greyscale images are detectable as soon as they have been created and then converted to binary?</p> <p>Many thanks.</p> <p><strong>UPDATE:</strong> My code for converting to greyscale is as follows:</p> <pre><code> try{ //Read in original image. BufferedImage inputImg = ImageIO.read(image); //Obtain width and height of image. double image_width = inputImg.getWidth(); double image_height = inputImg.getHeight(); //New images to draw to. BufferedImage bimg = null; BufferedImage img = inputImg; //Draw the new image. bimg = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_BYTE_GRAY); Graphics2D gg = bimg.createGraphics(); gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null); //Save new binary (output) image. String fileName = "greyscale_" + image.getName(); File file = new File("test_images\\Greyscale\\" + fileName); ImageIO.write(bimg, "jpg", file); } catch (Exception e){ System.out.println(e); } </code></pre> <p>How would I modify this to add the <code>flush()</code> and/or <code>close()</code> functions?</p> <p><strong>UPDATE:</strong> I have also created a line which prints after each successful conversion and the only feedback I have from the <code>binary</code> method is: <code>java.lang.NullPointerException (BINARY) test_images\Greyscale\desktop.ini: processed successfully. Binary image conversion complete.</code> whereas it should say: <code>(BINARY) images\298.jpg: processed successfully.</code>.</p> <p>Is there any reason for this? I don't understand why the <code>desktop.ini</code> file is read/processed?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload