Java - image output

Soldato
Joined
22 Oct 2005
Posts
2,884
Location
Moving...
I've got a 2D array of pixels that I want to output to screen. What's the best/easiest way to go about this?

At the minute I'm just using a buffered image which I've loaded in and just outputting the whole lot to screen and using JFrames and JPanels etc. Then I was using .setRGB to draw over the top but I've kind of reached the limit of what I can do with that.

Anyone know of any good tutorials that can walk me through how to do this?

Thanks.
 
Here you go RobH. It loads the image, creates an array of 2D pixels using .getRGB(), then outputs the image using various Swing components. Finally it draw a small line on the image using .setRGB to overwrite those particular pixels.

Code:
//Store the two input images as buffered images
    public void loadImages() throws FileNotFoundException, IOException{ 
        barcodeImage = getImage("8DigitsGrey.jpg");
    }
    
    
    //Read the images in
    public BufferedImage getImage(String filename) throws FileNotFoundException, IOException {
        
        File file = new File(filename);
        if(!file.exists())
            throw new FileNotFoundException(filename);
        
        barcodeImage = ImageIO.read(file);
        
        return barcodeImage;
    }
    
    
    //Create a 2Darray of pixels of the image
    public void buildArray() {
        
        int width = barcodeImage.getWidth();
        int height = barcodeImage.getHeight();
        pixelData = new int[width][height];
              
        for(int i=0; i<width; i++) {
            for(int j=0; j<height; j++) { 
                pixelData[i][j] = (barcodeImage.getRGB(i,j))&255; //getRGB returns the RGB value of the current pixel                   
            }         
        }
    }





    private static void  createAndShowGUI() throws FileNotFoundException, IOException {    
        
        JFrame frame = new JFrame("Image"); // Create the container        
        
        //Quit the application when this frame is closed
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  
        // Create and add the content
        Barcode panel = new Barcode();
        frame.add(panel);
        
        // Display the window
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        frame.setVisible(true); // Now the frame will appear on screen      
    }  
    
    
public Barcode() throws FileNotFoundException, IOException {
        
        loadImages();
        buildArray();
             
        JPanel imagePanel = new JPanel();
        imagePanel.setLayout(new BorderLayout());  
        picLabel = new JLabel(new ImageIcon(getImage("8Digits.jpg")));
        imagePanel.add("Center", picLabel);     
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        add("Center", imagePanel);

        drawSquare();    
    }


//Draws a line onto the image
    public void drawSquare(){
        
        barcodeImage.setRGB( 319, barcodeT, RED);
        barcodeImage.setRGB( 320, barcodeT, RED);
        barcodeImage.setRGB( 321, barcodeT, RED);
}
    
    
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        
                createAndShowGUI();
        
    }

Thanks.
 
Ok, I understand the code and see what it's doing but what are you trying to do and why is this code limiting you?
 
Ok, I understand the code and see what it's doing but what are you trying to do and why is this code limiting you?

I'll give you a quick overview of the project: We are given an image and are required to extract the ROI (Region of Interest) from the image. The image in question is a barcode (complete with background label) and the ROI is the number below it.

Now with my code I basically search the entire image's pixel data and cut out the background label that i'm not interested, then I want to cut out the bars leaving just the numbers, then seperate each number individually. Each time I seperate it (i.e. cut something out) I put the new area I am working with into a new array, so when I search it next I have less to search for. So this will leave me with 3 or 4 arrays of various sizes containing different areas.

It would just be a whole lot easier to display these arrays individually so I can check that i'm cutting out the right parts etc. I realise this is by far from the best way to go about it but its the easiest way to test it and make a working program. After I get that I can worry about making an efficient pretty program!

Cheers.
 
i dont know if this will be of any use...

I think this can be done in c# using the Bitmap class, it has a method (can't remember the name) that allows you to specify a region that you are interested in, as a rectangle, and another bitmap that it will draw it to.

I know its not Java but it might be worth looking for a similar 'Bitmap' class that allows resizing/cropping rather than worrying about drawing the image pixel by pixel?
 
Back
Top Bottom