Results 1 to 3 of 3

Thread: Array to image

  1. #1
    oxxxis is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default Array to image

    Hi, I am new to java programming, but i have this problem:

    I open an image, i put ir in array 2 dimensional, teh idea is like this


    array[count][0]=x;
    array[count][1]=y
    array[count][2]=red;
    array[count][3]=green;
    array[count][4]=blue;
    array[count][5]=alpha;


    I do so becaude i need to work with colors, but later on i want to save all changes in image. Can you please help me?! Some links with more info could be nice.

    Thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't use a 2-dimensional array but instead create a class to hold the information of each "row" with an int (x), int (y), and Color object and instead create a 1-D array of this or perhaps better an ArrayList of this. If you need more help, please provide us with more detail.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class Recon {
        BufferedImage image;
    
        private JPanel getContent() {
            initImage();
            int w = image.getWidth();
            int h = image.getHeight();
            int[] rgbs = new int[w*h];
            image.getRGB(0, 0, w, h, rgbs, 0, w);
            int[][] manArray = new int[rgbs.length][];
            for(int i = 0; i < manArray.length; i++) {
                int x = i%w;
                int y = i/w;
                int a = (rgbs[i] >> 24) & 0xff;
                int r = (rgbs[i] >> 16) & 0xff;
                int g = (rgbs[i] >>  8) & 0xff;
                int b = (rgbs[i] >>  0) & 0xff;
                manArray[i] = new int[] { x, y, r, g, b, a };
            }
            BufferedImage newImage = getNewImage(manArray);
            JPanel panel = new JPanel(new GridLayout(1,0));
            panel.add(new JLabel(new ImageIcon(image)));
            panel.add(new JLabel(new ImageIcon(newImage)));
            return panel;
        }
    
        private BufferedImage getNewImage(int[][] data) {
            manipulateData(data);
            int w = image.getWidth();
            int h = image.getHeight();
            BufferedImage altImage = getCompatibleImage(w, h);
            for(int i = 0; i < h; i++) {
                for(int j = 0; j < w; j++) {
                    int k = i*w + j;
                    int rgb = (data[k][5] & 0xff) << 24 |
                              (data[k][2] & 0xff) << 16 |
                              (data[k][3] & 0xff) <<  8 |
                              (data[k][4] & 0xff) <<  0;
                    altImage.setRGB(data[k][0], data[k][1], rgb);
                }
            }
            return altImage;
        }
    
        private void manipulateData(int[][] data) {
            int w = image.getWidth();
            int h = image.getHeight();
            Rectangle r = new Rectangle(w/4, h/4, w/2, h/2);
            for(int i = 0; i < h; i++) {
                for(int j = 0; j < w; j++) {
                    int k = i*w + j;
                    int x = data[k][0];
                    int y = data[k][1];
                    if(r.contains(x, y)) {
                        data[k][2] = 0;
                        data[k][3] = 255;
                        data[k][4] = 0;
                    }
                }
            }
        }
    
        private void initImage() {
            int w = 100;
            int h = 75;
            image = getCompatibleImage(w, h);
            Graphics2D g2 = image.createGraphics();
            g2.setPaint(new GradientPaint(0, 0, Color.red,
                                          w, h, Color.blue));
            g2.fillRect(0,0,w,h);
            g2.dispose();
        }
    
        private BufferedImage getCompatibleImage(int w, int h) {
            return GraphicsEnvironment.getLocalGraphicsEnvironment()
                                      .getDefaultScreenDevice()
                                      .getDefaultConfiguration()
                                      .createCompatibleImage(w, h);
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame("Reconstruction");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Recon().getContent());
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. Reconstruct image from a pixel array
    By Sepho in forum New To Java
    Replies: 0
    Last Post: 08-29-2009, 12:27 AM
  2. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-11-2009, 11:33 PM
  3. get image file from array of pixels
    By mester in forum Advanced Java
    Replies: 1
    Last Post: 12-28-2008, 10:38 AM
  4. 2d array to image
    By Nina* in forum New To Java
    Replies: 1
    Last Post: 10-01-2008, 04:08 PM
  5. 2D Array to image
    By fred in forum Java 2D
    Replies: 1
    Last Post: 07-24-2007, 01:52 AM

Posting Permissions

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