Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Exclamation Update existing buffered image

    I have a question....
    If I have a buffered image. I apply some changes to the image, such as smoothen the image. How can I display the new image. Here is a snippet of my source code. Because everytime I apply the the new image. The only thing is see is my original image. What are the few things to keep in mind to update an image(i.e.)after using setrgb()

    Java Code:
        class MyPanel extends JPanel
        {
            //Purpose: a panel in which to draw
    
            //global offscreen and the Graphics2D variable g2
            private BufferedImage bufImag;
            protected Graphics2D g2;
            private int x,y;
            final int offScrWidth=1500,offScrHeight=1500;
    
            public MyPanel()
            {
     
    
              bufImag = new BufferedImage(offScrWidth,offScrHeight,BufferedImage.TYPE_INT_RGB);
    		  try
    		  {
    			  InputStream in = new FileInputStream("Skull.JPG");
    			  bufImag = ImageIO.read(in);
    		  }
    		  catch (IOException ex) {}
    
              g2 = bufImag.createGraphics();
    
              setPreferredSize(new Dimension(offScrWidth,offScrHeight));
    
    
    
    		g2.drawImage(bufImag, 0,0,null);
         	}
    
    		public void smoothme ()
    		{
    		  	  int sum=0;
    			  int width=bufImag.getWidth();
    			  int height=bufImag.getHeight();
    			  int[][] pixels = new int[width][height];
    
    
    		  	  int I,J,i,j;
    				for (J=0;J<height;J++)
    				{
    					for (I=0;I<width;I++)
    					{
    						pixels[I][J] = (bufImag.getRGB(I,J)& 0x000000ff);
    					 }
    				}
    
    			  bufImag = new BufferedImage(offScrWidth,offScrHeight,BufferedImage.TYPE_INT_RGB);
    	//set of codes for processing the image
    
    				for (J=0;J<width;J++)
    				{
    					for (I=0;I<height;I++)
    					{
    						bufImag.setRGB(I,J,((pixels[I][J]<<8)+pixels[I][J]<<8)+pixels[I][J]);
    					 }
    				}
    
    			   g2.drawImage(bufImag, 0,0,null);
    			   repaint();
    
    
    		}
    		  public void paintComponent(Graphics g)
    		  {
    			//Purpose: do a complete screen refresh here
    			//Input: a Graphics to draw on
    			//Output: changes to the Graphics
    
    		    super.paintComponent(g);
    		    Graphics2D g2 = (Graphics2D)g;//copy offscreen to screen
    		    g2.drawImage(bufImag, null,0,0);
    		  }
    		}
    		class smooth{
    		   // Main class
    		   public static void main(String[] args){
    
    		      MyPanel     myPanel=new MyPanel();
    		      MyFrame     myFrame=new MyFrame(myPanel);
    
    		      System.out.println("\n\nEnd of Processing");
    		   }
    		}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems you don't have write that buffered image into the required format.

    Read about the following.

    Java Code:
    ImageIO.write()

  3. #3
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    I didnt understand. If you observe. I read with
    Java Code:
    ImageIO.read(in).
    Soent
    Java Code:
    g2.drawImage(bufImag, 0,0,null)
    confirm that image has been converted to buffered image?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    After read and do the modifications on the image you have a BufferedImage, is that right?

    Now how can you save it as an image? Write it to ImageIO.

    Java Code:
    ImageIO.write(bufferedImage, "JPEG", 
                        new File("images/new_image.jpeg"));

  5. #5
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    No you are getting my question wrong.
    I create a formula to be applied to the pixels of the image. I even do the setRGB method. but when I give the drawImage as in the last line of the smoothme function. I am still getting my original image, not the modified image.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    the thing is that I am creating a menu-driven application, with the option of 'smooth the image' on the menu. When this option is clicked the image has to be smoothed, which it is not.

    My formula is not wrong, because if I load the image and directly apply the formula to it and display it. It is working,

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    This collects information of the image
    Java Code:
    				for (J=0;J<height;J++)
    				{
    					for (I=0;I<width;I++)
    					{
    						pixels[i][J] = (bufImag.getRGB(I,J)& 0x000000ff);
    					 }
    				}
    This applies the formula to the pixels.
    Java Code:
    			for (J=0;J<width;J++)
    				{
    					for (I=0;I<height;I++)
    					{
    						bufImag.setRGB(I,J,((pixels[i][J]<<8)+pixels[i][J]<<8)+pixels[i][J]);
    					 }
    				}
    
    			   g2.drawImage(bufImag, 0,0,null);
    			   repaint();
    These are the last statements in the smoothme function

  10. #10
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    This is the code for the attaining new pixel values
    Java Code:
          for (i=0;i<width;i++)
          {
              for (j=0;j<height;j++)
              {
                  value = (int)Math.round(pixels[i][j]);
                  count = data[value];
                  data[value]=count+1;
               }
          }
    
    
          for(value=0;value<data.length;value++)
          {
    		  Havg+=data[value];
    	  }
    	  Havg=Havg/data.length;
    	  i=0;
    	  j=0;
    		  for(value=0;value<data.length;value++)
    		  {
    			  low=high;
    			  cum=cum+data[value];
    			  high=cum/Havg;
    			  func[value]=(int)Math.round((low+high)/2);
    		  }
    This comes before applying the pixels to the image.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In the pixels array, all the pixels are set to 255. Is that right? I cannot see that any different values there. If the image read correctly, every pixel cannot have the same value, isn't it?

  12. #12
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    Yes that was another doubt I had. When I printed the new 'pixel values'. it was 255. But when I directly applied this to the image, I had a lighter image, with higher details displyed. How could that be possible?

    But this does not happen, if I apply it to the program, and when I click smooth image, nothing happens, I just see the original image.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Reason is your code not effected on any modifications. Start from the first line and see what happen.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What's the achievement are you looking on the bitwise & operation?

    Java Code:
    pixels[i][J] = (bufImag.getRGB(I,J)& 0x000000ff);

  15. #15
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    made a mistake while uploading the code to you. This is the code to apply pixels:

    Java Code:
    	  for(i=0;i<width;i++)
    	  {
    		  for(j=0;j<height;j++)
    		  {
    			  value = (int)pixels[i][j];
    			  bufImag.setRGB(i,j,((func[value]<<8)+func[value]<<8)+func[value]);
    
    		  }
    	  }*/

  16. #16
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    The achievement Im looking for is just taking the colors, BGR, masking all other values. Trust me that is correct.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems you are using the drawImage() in wrong way. Is that first argument is BufferedImage, or Image? I've not remember what's say on the Java doc about that, is that right?

  18. #18
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    check this. I have a number of windows opened here. So Im making a mistake again. THis is the code that smoothens the image and setRGB
    Java Code:
    			  for (J=0; J<=height-1; J++)
    			  {
    				  for (I=2; I<=width-3; I++)
    				  {
    
    					  sum=0;
    
    					  sum=(pixels[I-2][J] + 2*pixels[I-2][J] + 4*pixels[I][J] + 2*pixels[I+1][J] + pixels[I+2][J])/10;
    					  pixels[I][J] = sum;
    				  }
    			  }
    
    				for (J=0;J<width;J++)
    				{
    					for (I=0;I<height;I++)
    					{
    						bufImag.setRGB(I,J,((pixels[I][J]<<8)+pixels[I][J]<<8)+pixels[I][J]);
    					 }
    				}
    
    			   g2.drawImage(bufImag, 0,0,null);
    			   repaint();

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    With your new code segment it didn't execute the following line of code.

    Java Code:
    g2.drawImage(bufImag, 0,0,null);
    Comment that following and try to run,

    Java Code:
    				for (J=0;J<width;J++)
    				{
    					for (I=0;I<height;I++)
    					{
    						bufImag.setRGB(I,J,((pixels[i][J]<<8)+pixels[i][J]<<8)+pixels[i][J]);
    					 }
    				}

  20. #20
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    did not understand. if I comment
    Java Code:
    g2.drawImage(bufImag, 0,0,null);
    I get the same result as with that code.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 08-13-2008, 10:46 PM
  2. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 03:49 PM
  3. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 08:05 PM
  4. How to update Existing Word Doc Using java
    By umashankar in forum Advanced Java
    Replies: 1
    Last Post: 07-27-2007, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •