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

    Default Writing integer pixel array(Range:0-255) into .txt file

    Problem has been by solved Mr Norm
    Last edited by Mazharul; 08-25-2008 at 10:47 AM. Reason: Problem has been by solved Mr Norm

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    What format do you want your data to be in the output file?
    Why do you write bytes to a .txt file? .txt files are normally ASCII characters (ie what can be typed in via a keyboard). Bytes 0-255 won't be edittable.
    Look at the FileOutputStream class for writing bytes to a file.
    writeInt() is not working
    What does not working mean? Please explain more.

    For working on a problem like this, write a small, simple program that writes bytes from your program and then reads them back and checks them against what was written to verify your logic. Also you could look at the output file in a hex editor to verify the output.

    Also use CODE & /CODE in [] tags to post your code.

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class BufferConversion {
        private JScrollPane getContent(BufferedImage image) {
            int[][] data = extractData(image);
            String path = "bufferConversion.txt";
            writeToFile(data, path);
            JLabel label = new JLabel(new ImageIcon(image), JLabel.CENTER);
            return new JScrollPane(label);
        }
    
        private int[][] extractData(BufferedImage image) {
            int w = image.getWidth();
            int h = image.getHeight();
            System.out.printf("w = %d  h = %d%n", w, h);
            WritableRaster raster = image.getRaster();
            int[][] data = new int[h][w];
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    data[y][x] = raster.getSample(x, y, 0);
                }
            }
            return data;
        }
    
        private void writeToFile(int[][] data, String path) {
            int h = data.length;
            int w = data[0].length;
            int[] array = new int[h*w];
            for(int y = 0; y < h; y++) {
                for(int x = 0; x < w; x++) {
                    int index = y*w + x;
                    array[index] = data[y][x];
                }
            }
            try {
                File file = new File(path);
                BufferedWriter bw = new BufferedWriter(
                                    new OutputStreamWriter(
                                    new FileOutputStream(file)));
                
                for(int i = 0; i < array.length; i++) {
                    String s = String.valueOf(array[i]) + " ";
                    bw.write(s, 0, s.length());
                }
                bw.close();
            } catch(IOException e) {
                System.out.println("write error: " + e.getMessage());
            }
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/wolf.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new BufferConversion().getContent(image));
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

Similar Threads

  1. Scanning Image Pixel by Pixel
    By the_transltr in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2012, 04:01 PM
  2. How 2d Pixel Arrary Is Stored In .txt/.dat File
    By Mazharul in forum New To Java
    Replies: 1
    Last Post: 08-18-2008, 12:23 PM
  3. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 10:59 PM
  4. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 06:09 AM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 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
  •