3 Replies - 2840 Views - Last Post: 15 September 2012 - 02:04 PM Rate Topic: -----

#1 amy310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-November 10

image processing code

Posted 15 September 2012 - 12:51 PM

 import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.Random;



public class BasicDisplay_2 extends Component{

  private BufferedImage originalPic;
  private Raster picRasterArray;
  private DataBuffer picArray;

  public void paint(Graphics g) {
     g.drawImage(originalPic, 0, 0, null);
  }

  public BasicDisplay_2() {
   try {

       originalPic = ImageIO.read(new File("C://Users//Lily//Desktop//blue_balloons.jpg"));


       picRasterArray = originalPic.getRaster();


       System.out.println("************Image Properties***********");
       System.out.println("Height in pixels: " + picRasterArray.getHeight());
       System.out.println("Width in pixels: " + picRasterArray.getWidth());
       System.out.println("Number of Color Bands: " + picRasterArray.getNumBands());

       SampleModel st = picRasterArray.getSampleModel();
       System.out.println("Image Sample Model: "+ st);


       picArray = picRasterArray.getDataBuffer();
      System.out.println("Total number of array elements in model: " +
                             picArray.getSize());


       for(int i = 0; i<510; ++i){
         st.setSample(i,300,0,0,picArray);
         st.setSample(i,300,1,0,picArray);
         st.setSample(i,300,2,0,picArray);
       }

       int []change = {255, 255, 255};


       int randomRow, randomColumn;

       Random randomGenerator = new Random(); //declartion of random generator object

       for(int n=0; n<=80000; ++n){
         randomColumn = randomGenerator.nextInt(510);    //generate random column
         randomRow = randomGenerator.nextInt(980);      //generate random row
         st.setPixel(randomColumn,randomRow,change,picArray);  //change the pixel color
                                                     //at the row and column identified
        }
       } catch (IOException e) {
      }
    }

    public Dimension getPreferredSize() {
        if (originalPic == null) {
             return new Dimension(800,700);
        } else {
           return new Dimension(originalPic.getWidth(null),
originalPic.getHeight(null));
       }
    }

    public static void main(String[] args) {

        JFrame f = new JFrame("Simple Image Processing");

        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        f.add(new BasicDisplay_2());
        f.pack();
        f.setVisible(true);
    }
} 


so this was an in class exercise ungraded , there were three parts 1st was learning how to load an image from an external file and display in a simple frame , 2nd part added obtaining basic properties of the image and 3rd which is d code above was supposed to add white noise to the image as well as draw a line through the image by changing one row to black. this code compiled without errors.

this is the error i am getting as you can see it does part of what its supposed to do but i can't figure out the "out of bounds exception"

************Image Properties***********
Height in pixels: 300
Width in pixels: 400
Number of Color Bands: 3
Image Sample Model: java.awt.image.PixelInterleavedSampleModel@305b002
Total number of array elements in model: 360000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at java.awt.image.ComponentSampleModel.setSample(ComponentSampleModel.java:1075)
at BasicDisplay_2.<init>(BasicDisplay_2.java:52)
at BasicDisplay_2.main(BasicDisplay_2.java:93)

Process completed.

Is This A Good Question/Topic? 0
  • +

Replies To: image processing code

#2 amy310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-November 10

Re: image processing code

Posted 15 September 2012 - 01:39 PM

if anyone has any suggestions as to what may be wrong i'd really appreciate it .
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,235
  • Joined: 11-December 07

Re: image processing code

Posted 15 September 2012 - 01:54 PM

Looks like you are looping over 510 pixels but the image is only 400 pixels wide. If you can tell using code how large the image is then you should use that data in your for loop instead of hard coding it.
Was This Post Helpful? 2
  • +
  • -

#4 amy310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-November 10

Re: image processing code

Posted 15 September 2012 - 02:04 PM

View Postcfoley, on 15 September 2012 - 01:54 PM, said:

Looks like you are looping over 510 pixels but the image is only 400 pixels wide. If you can tell using code how large the image is then you should use that data in your for loop instead of hard coding it.


hey thanks good suggestion . will try that :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1