Results 1 to 2 of 2
  1. #1
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default How 2d Pixel Arrary Is Stored In .txt/.dat File

    HELLO EVERY BODY...............

    I want to store the 2D int[][] samples array corresponding to an image in a “.txt/.dat” file.how this 2d array will be converted to a 1 dimensional array.I could not do this because I do not know the 2d array dimension.Is it possible to show this 2d array in in Jframe or any visual window. Acctually I want to see later the calculated value of histogram.
    My code is….it is error free


    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    import javax.imageio.stream.*;



    class Image {
    protected int width,height;
    // 'samples' stores the image pixel values.
    protected int[][] samples;
    // Constructor: Reads the image from the
    // specified file name.
    public Image(String filename)
    throws Exception { read(filename); }
    // Returns the pixel width of the image.
    public int getWidth() { return width; }
    // Returns the pixel height of the image.
    public int getHeight() { return height; }
    // Reads the image from the specified file
    // name into the 'samples' array. Throws an
    // exception if the image is stored in an
    // unsupported file format (currently only
    // .GIF, .JPG, and .PNG are supported by Sun).
    public void read(String filename)
    throws Exception {
    // Extract the file name suffix.
    String ext = filename.substring
    (filename.indexOf('.')+1);
    // Create a file object for the file name.
    File fileImage = new File(filename);
    // Get a list of ImageReaders that claim
    // to be able to decode this image file
    // based on the file name suffix.
    Iterator imageReaders = ImageIO.
    getImageReadersBySuffix(ext);
    ImageReader imageReader;
    // Grab the first ImageReader in the list.
    if (imageReaders.hasNext())
    imageReader = (ImageReader)
    imageReaders.next();
    // If we get here we cannot decode the image.


    // Draws the image stored in the 'samples'
    // array on the specified graphics context.
    public void draw(Graphics gc,int x,int y){
    BufferedImage bufImage =
    createBufferedImage();
    gc.drawImage(bufImage,x,y,null);
    }
    // Converts the 'samples' array into a
    // BufferedImage object. Students do not have
    // to understand how this works.
    private BufferedImage
    createBufferedImage() {
    // Create a monochrome BufferedImage object.
    BufferedImage bufImage = new
    BufferedImage(width,height,
    BufferedImage.TYPE_BYTE_GRAY);
    // Create a WriteableRaster object so we can
    // put sample data into the BufferedImage
    // object's raster.
    WritableRaster wRaster =
    bufImage.getRaster();
    // Copy the 'samples' data into the
    // BufferedImage object's raster.
    for (int row = 0; row < height; row++)
    for (int col = 0; col < width; col++)
    wRaster.setSample
    (col,row,0,samples[row][col]);
    // Return the newly created BufferedImage.
    return bufImage;
    }
    } // End of Class Image






    Main program
    import java.awt.*;
    import javax.imageio.*;
    class TestImage {
    public static void main(String args[])
    throws Exception {
    // Create a frame to display the image.
    Frame frame = new Frame("Test Image");
    frame.setSize(1024,768);
    frame.setVisible(true);
    Graphics gc = frame.getGraphics();
    try {
    // Read the image from the file.
    Image img = new Image("Horse.jpg");
    // Display the image.
    img.draw(gc,10,40);
    // Flip the image upside down
    img.flipX();
    // Display the flipped image.
    img.draw(gc,20+img.getWidth(),40);
    // Write the new image to a file
    img.write("HorseNew.jpg");
    } catch (Exception e) {
    System.out.println
    ("Exception in main() "+e.toString());
    }
    }
    }

  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

    There is no direct way to convert 2D array into one dimensional array. But it's possible to define a logic your self to do it. But you have a problem there, don't know about the dimensions in the array. But, how did you use an array(as you explain an int type simple array) for this without knowing the size(in the word dimension you mean size?) . Did you define it in a larger size?

Similar Threads

  1. Scanning Image Pixel by Pixel
    By the_transltr in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2012, 05:01 PM
  2. Replies: 0
    Last Post: 07-11-2008, 05:43 PM
  3. Stored Procedures
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 03:34 AM
  4. Replies: 1
    Last Post: 07-17-2007, 05:10 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
  •