Issue when attempting to create black and white image from previously processed image using file I/O in Java -


i making application takes image , applies grayscale filter using file i/o. user asked threshold , save location take processed image , make pure black , white. issue having when 2nd image created , try open it, windows reports file damaged though file size same processed image seems working correctly. here code application. continue using file io create this, understand java has built in function creating binary image.

import java.io.*; import javax.swing.*;  public class bitmapper {     public static void main(string[] args)     {         string threshold;         int thresholdint;         jfilechooser chooser1 = new jfilechooser();         jfilechooser chooser2 = new jfilechooser();         jfilechooser chooser3 = new jfilechooser();         int status1 = chooser1.showopendialog(null);         int status2 = chooser2.showsavedialog(null);         if(status1 == jfilechooser.approve_option && status2 == jfilechooser.approve_option)         {              try             {                 // handling binary (not text) data, use fileinputstream                 fileinputstream in  = new fileinputstream(chooser1.getselectedfile());                 fileoutputstream out = new fileoutputstream(chooser2.getselectedfile() + "_gray.bmp");                  int = 0;                 int counter = 0;                   while((i=in.read())!=-1)                  {                     if (++counter>54)   // skip past bitmap headers                     {                         int b = i;                         int g = in.read();                         int r = in.read();                          int gray = (b + g + r)/3;                          out.write(gray);                         out.write(gray);                         = gray;                     }                     out.write(i);                 }                  out.close();                 in.close();                 threshold = joptionpane.showinputdialog(null, "please enter threshold turn picture black , white.");                  try                 {                     thresholdint = integer.parseint(threshold);                     int status3 = chooser3.showsavedialog(null);                     if(status3 == jfilechooser.approve_option)                     {                         in = new fileinputstream(chooser2.getselectedfile() + "_gray.bmp");                         out = new fileoutputstream(chooser3.getselectedfile() + "_bw.bmp");                         while((i=in.read())!=-1)                          {                             if (++counter>54)   // skip past bitmap headers                             {                                 int b = i;                                 int g = in.read();                                 int r = in.read();                                  if(b > thresholdint)                                     out.write(0);                                 else                                     out.write(255);                                  if(g > thresholdint)                                     out.write(0);                                 else                                     out.write(255);                                  if(r > thresholdint)                                     = 0;                                 else                                     = 255;                              }                             out.write(i);                         }                      }                     else                         joptionpane.showmessagedialog(null, "you did not select save location second image.");                 }                   catch(numberformatexception ex){                     joptionpane.showmessagedialog(null, "issue user input, ensure entered integer. error: " + ex);                 }                }             catch(ioexception ex)             {                 joptionpane.showmessagedialog(null,"error in input/output of file:" + " '" + ex + "'");             }          }         else             joptionpane.showmessagedialog(null,"you did not specify file or save location new file.");      }  } 

your problem not resetting 0 counter variable after creating _gray image, before creating _bw one. therefore reading / writing headers color bytes, tresholding them , corrupting them. resetting should fix it.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -