Results 1 to 13 of 13
  1. #1
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Cool Conversion of any image file to text file in Java...'

    The following code reads the image file and store its Pixel values(ARGB) in text file....'
    A - Alpha'
    R - Red'
    G - Green'
    B - Blue'
    '
    ----------------------------------------------------------------------------------------------------------------------------------------------
    Java Code:
    package imageScrambler.image_encrypt;
    
    import java.awt.image.*;
    import javax.imageio.ImageIO;
    import java.io.*;
    
    public class e1_convert
    {
        public static void main(String[] args) throws FileNotFoundException, IOException
        {
            File output_file=new File("D:/pixels.txt");                   //Output Text file
            PrintStream out=new PrintStream(new FileOutputStream(output_file));
            System.setOut(out);
            /*
             * If you try this code,
             * take image of 30 x 30 dimensions'
             * as it takes less time to execute...'
             */
            BufferedImage image = readImage("D:/rainbow.jpg");               //Input Image file
            printAllARGBDetails(image);
        }
        
        public static void printAllARGBDetails(BufferedImage image)
        {
            int width = image.getWidth();
            int height = image.getHeight();
            int pix_num=1;
            int total_pix=width*height;
            System.out.println("--------------------------------------------");
            System.out.println("Image Scrambler");   
            System.out.println("Image Dimension: Height-" + height + ", Width-" + width);
            System.out.println("Total Pixels: " + (height * width));
            System.out.println("--------------------------------------------");
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    int pixel = image.getRGB(i, j);
                    String idata=(getARGBPixelData(pixel));
                    System.out.print("Pixel ("+i+","+j+"): ");
                    System.out.print(idata);
                    if(pix_num<total_pix)       //To delete the line that generates at end of file
                    {
                        System.out.println("");
                    }
                    pix_num++;
                }
            }
        }
    
        public static String getARGBPixelData(int pixel)
        {
            String ARGBvalue="";
            int alpha = (pixel >> 24) & 0x000000FF;
            int red = (pixel >> 16) & 0x000000FF;
            int green = (pixel >>8 ) & 0x000000FF;
            int blue = (pixel) & 0x000000FF;
            ARGBvalue = alpha + " " + red + " " + green + " " + blue;
            return ARGBvalue;
        }
        
        public static BufferedImage readImage(String fileLocation) throws IOException
        {
            BufferedImage img = null;
            img = ImageIO.read(new File(fileLocation));
            return img;
        }
    }
    ----------------------------------------------------------------------------------------------------------------------------------------------
    '
    Now the problem is that , I want input as text file "pixels.txt" and after executing the program, the output would be original image again as "rainbow.jpg"
    I am unable to get idea how should I start...?
    Plz help..............................'
    I m stuck....'
    '.................................................. .........'
    Last edited by suyog53; 09-21-2012 at 02:34 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Conversion of any image file to text file in Java...'

    Youre printing a lot of extra data into that file.
    Are you sure you need the pixel coordinates, because you should be able to figure that out simply from a width/height provided in the header of your txt file.

    Anyway, you need to read each pixel line in and convert that into a single pixel int from the 4 ints represented on the line.
    That's two tasks there...getting the 4 values from the file for a single pixel.
    Turning those 4 values into a single int.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Conversion of any image file to text file in Java...'

    @Tolls'
    '
    No..the other data is not important.....................'
    Only the pixel values...........'
    "255 123 223 186 255 . . . . . . . 234 13 54 26"
    '
    That's two tasks there...getting the 4 values from the file for a single pixel.
    '
    Yes....your idea is best, but...
    If I use to read each byte to cover the 4 int values, then it gives problem because pixels vary from 1 byte to 3 byte'
    So, it is difficult to guess how much bits to cover in a single stroke...'

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

    Default Re: Conversion of any image file to text file in Java...'

    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Conversion of any image file to text file in Java...'

    Um, you do the reverse of what you did to save the ARGB values.
    And I didn't realise it was all on one line.
    Anyway, you are reading these in as Strings and converting to ints.
    AFter all, you wrote them out as Strings.

    So write some code that will work with a sample file containing a few pixels of data for, say a 10x10 single-coloured image.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Conversion of any image file to text file in Java...'

    Um, you do the reverse of what you did to save the ARGB values.
    And I didn't realise it was all on one line.
    yes.............perfectly guessed'
    '
    So
    write some code
    that will work with a sample file containing a few pixels of data for, say a 10x10 single-coloured image.
    '
    Thats what i m trying .... but got my head confused...'

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Conversion of any image file to text file in Java...'

    About what though?
    You haven't provided any code here to show what you've tried.
    I mean, you've managed to write the pixel data out, so read it in, even if it's as simple as reading the entire line in as a single String and simply doing a split() call and then loop through the String[] turning them into ints.

    That sentence there should be enough for you to give it a go.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Conversion of any image file to text file in Java...'

    even if it's as simple as reading the entire line in as a single String and simply doing a split() call and then loop through the String[] turning them into ints.
    k'
    m working on it...'
    will get if problem arises...'

  9. #9
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Conversion of any image file to text file in Java...'

    Thanx frnz for posts.........................'
    Now, the pixel value is stored in an array....'
    '
    Now, my output is:
    '
    Pixel 1: 255 200 191 231
    Pixel 2: 255 237 028 036
    Pixel 3: 255 034 177 076
    Pixel 4: 255 085 140 066
    Pixel 5: 255 200 191 231
    Pixel 6: 255 237 028 036
    Pixel 7: 255 231 188 167
    Pixel 8: 255 237 028 036
    Pixel 9: 255 237 028 036
    Pixel 10: 255 063 072 204
    Pixel 11: 255 226 125 144
    Pixel 12: 255 063 072 204
    Pixel 13: 255 200 191 231
    Pixel 14: 255 062 187 099
    Pixel 15: 255 255 127 039
    Pixel 16: 255 255 127 039
    Pixel 17: 255 200 191 231
    Pixel 18: 255 212 177 189
    Pixel 19: 255 063 072 204
    Pixel 20: 255 040 158 100
    Pixel 21: 255 034 177 076
    Pixel 22: 255 237 028 036
    Pixel 23: 255 248 253 249
    Pixel 24: 255 165 169 231
    Pixel 25: 255 200 191 231
    Pixel 26: 255 130 142 059
    Pixel 27: 255 207 183 207
    Pixel 28: 255 200 191 231
    Pixel 29: 255 225 219 242
    Pixel 30: 255 255 127 039
    Pixel 31: 255 212 147 134
    Pixel 32: 255 063 072 204
    Pixel 33: 255 200 191 231
    Pixel 34: 255 034 177 076
    Pixel 35: 255 198 106 046
    Pixel 36: 255 153 098 126
    Pixel 37: 255 233 239 248
    Pixel 38: 255 200 191 231
    Pixel 39: 255 237 028 036
    Pixel 40: 255 255 127 039
    Pixel 41: 255 200 191 231
    Pixel 42: 255 111 086 163
    Pixel 43: 255 255 127 039
    Pixel 44: 255 255 255 255
    Pixel 45: 255 086 082 180
    Pixel 46: 255 034 177 076
    Pixel 47: 255 255 127 039
    Pixel 48: 255 255 255 255
    Pixel 49: 255 255 127 039
    Pixel 50: 255 147 153 227
    Pixel 51: 255 255 127 039
    Pixel 52: 255 067 076 205
    Pixel 53: 255 200 191 231
    Pixel 54: 255 230 115 130
    Pixel 55: 255 204 187 219
    Pixel 56: 255 237 028 036
    Pixel 57: 255 207 191 219
    Pixel 58: 255 200 191 231
    Pixel 59: 255 255 127 039
    Pixel 60: 255 063 072 204
    Pixel 61: 255 063 072 204
    Pixel 62: 255 063 072 204
    Pixel 63: 255 068 071 199
    Pixel 64: 255 192 184 230
    Pixel 65: 255 255 127 039
    Pixel 66: 255 223 165 153
    Pixel 67: 255 200 191 231
    Pixel 68: 255 255 135 053
    Pixel 69: 255 076 192 110
    Pixel 70: 255 255 127 039
    Pixel 71: 255 175 169 226
    Pixel 72: 255 200 191 231
    Pixel 73: 255 041 174 077
    Pixel 74: 255 063 072 204
    Pixel 75: 255 034 177 076
    Pixel 76: 255 063 072 204
    Pixel 77: 255 063 072 204
    Pixel 78: 255 038 164 092
    Pixel 79: 255 063 072 204
    Pixel 80: 255 201 111 086
    Pixel 81: 255 255 255 255
    Pixel 82: 255 235 038 048
    Pixel 83: 255 144 216 165
    Pixel 84: 255 196 187 230
    Pixel 85: 255 255 255 255
    Pixel 86: 255 225 163 147
    Pixel 87: 255 041 154 104
    Pixel 88: 255 034 177 076
    Pixel 89: 255 131 155 060
    Pixel 90: 255 187 065 046
    Pixel 91: 255 237 028 036
    Pixel 92: 255 237 238 250
    Pixel 93: 255 063 072 204
    Pixel 94: 255 034 177 076
    Pixel 95: 255 059 085 189
    Pixel 96: 255 117 184 154
    Pixel 97: 255 200 191 231
    Pixel 98: 255 245 100 085
    Pixel 99: 255 159 163 229
    Pixel 100: 255 245 121 125
    '
    How can i put the value of pixels and create image?'....i.e.how to implement values separately to create image?'
    Does any1 know how to create and where to store created image?'

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

    Default Re: Conversion of any image file to text file in Java...'

    How many forums are you asking this very same question?

  11. #11
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Conversion of any image file to text file in Java...'

    on this site and one other?'
    Does this matter?'

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

    Default Re: Conversion of any image file to text file in Java...'

    Quote Originally Posted by suyog53 View Post
    on this site and one other?'
    Does this matter?'
    Cross-posting itself doesn't matter, but not informing folks who are helping you in all the involved forums does matter. Otherwise someone in another forum risks wasting time posting answers that have already been posted in a cross-post. If you respect the time of volunteers helping you here and elsewhere, you'll notify all of each cross-post. Failure to do this can get you placed on some volunteer's do-not-help list.

  13. #13
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

Similar Threads

  1. conversion of text/log file to html
    By c0thane in forum New To Java
    Replies: 0
    Last Post: 08-10-2012, 11:57 PM
  2. Image file to binary file conversion
    By Mantra in forum New To Java
    Replies: 5
    Last Post: 11-29-2010, 01:59 PM
  3. Image file to Text Conversion
    By sureshsoftengg in forum New To Java
    Replies: 2
    Last Post: 09-21-2010, 01:08 PM
  4. Conversion of image object into file
    By aartheesrini in forum Java Applets
    Replies: 0
    Last Post: 03-12-2009, 09:09 AM
  5. Regarding conversion of Image file to Video file
    By RamaDeviMsc in forum Advanced Java
    Replies: 0
    Last Post: 06-25-2007, 03:28 PM

Tags for this Thread

Posting Permissions

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