1 2 Previous Next 26 Replies Latest reply: Aug 2, 2007 7:32 AM by 807605 Go to original post RSS
      • 15. Re: Convert int[] to BufferedImage
        thomas.behr
        Your above statement implies to me that I should be
        able to combine all the color information for each
        pixel into a single int. When I first wrote the code,
        I tried that approach but could not get it to work.
        How can my code example be modified to accomplish
        this.
        You mean something like this?
        import java.awt.image.BufferedImage;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.ImageIcon;
        
        public class ImageTest {
        
          private ImageTest() {
          }
        
          public static void main( String[] args ) {
            final BufferedImage img = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
            for (int i = 0; i < 20; ++i) {
              for (int j = 0; j < 20; ++j) {
                img.setRGB(i, j, 0xffff0000); // all pixels opaque red
              }
            }
        
            final JFrame frame = new JFrame(ImageTest.class.getName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new JLabel("An image", new ImageIcon(img), JLabel.LEFT));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
          }
        }
        • 16. Re: Convert int[] to BufferedImage
          camickr
          You mean something like this?
          Well, that should help the OP solve his problem.

          I guess the answer to the question is that you can't use the array directly. You need to take the information from the array and manipulate it to get the pixel color in an int variable and then update the pixel directly.
          • 17. Re: Convert int[] to BufferedImage
            thomas.behr
            I guess the answer to the question is that you can't
            use the array directly. You need to take the
            information from the array and manipulate it to get
            the pixel color in an int variable and then update
            the pixel directly.
            Well, actually you can take the array directly, but it's ugly:
            import java.awt.Point;
            import java.awt.image.BufferedImage;
            import java.awt.image.DataBufferInt;
            import java.awt.image.Raster;
            
            public class ImageFactory {
              private static final BufferedImage PROTOTYPE = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            
              public static BufferedImage createImage( final int[] i, final int width, final int height ) {
                return new BufferedImage(
                        PROTOTYPE.getColorModel(),
                        Raster.createWritableRaster(
                                PROTOTYPE.getSampleModel().createCompatibleSampleModel(width, height),
                                new DataBufferInt(i, i.length),
                                new Point()),
                        false,
                        null);
              }
            
              private ImageFactory {
              }
            }
            • 18. Re: Convert int[] to BufferedImage
              thomas.behr
              int[] is a much better (and more intuitive) solution over byte[]
              Oh, really? So, you're saying that you think of ARGB color values in terms of ints like
              -2130771968
              -2147418368
              -2147483393
              instead of in terms of bytes as in
              128 255   0   0
              128   0 255   0
              128   0   0 255
              ?
              Well, to me the int values mean nothing, the bytes on the other hand clearly tell me that these are 50% transparent red, green, and blue, respectively. Of course I only recognize well-known colors or color tendencies this way, but that's more than I can say for the ints.

              Ironically, you explain int color values by breaking them down into bytes.
              • 19. Re: Convert int[] to BufferedImage
                807605
                int[] is a much better (and more intuitive)
                solution over byte[]

                Oh, really? So, you're saying that you think of ARGB
                color values in terms of ints like
                -2130771968
                -2147418368
                -2147483393
                instead of in terms of bytes as in
                128 255   0   0
                128   0 255   0
                128   0   0 255
                ?
                that makes no sense
                i already said the int consists of 4 bytes
                so not sure where you're getting these values which are larger than 255
                Well, to me the int values mean nothing, the bytes on
                the other hand clearly tell me that these are 50%
                transparent red, green, and blue, respectively. Of
                course I only recognize well-known colors or color
                tendencies this way, but that's more than I can say
                for the ints.
                again, you're assuming i'm advocating ints to represent color and i'm not
                i'm advocating using ints to represent pixels
                there's a difference
                Ironically, you explain int color values by breaking
                them down into bytes.
                now you're getting it
                • 20. Re: Convert int[] to BufferedImage
                  807605
                  Which to me seems totally irrelevant if you want
                  to
                  go from int[] to BufferedImage ...
                  you're right, because there's not a setRGB()
                  method
                  or anything
                  you guys are all killing me...
                  Wtf? If that is supposed to be ironic or sarcastic,
                  you really should make that more ... er ... obvious.
                  not sure what you're saying here
                  are you saying the Java API doesn't support these methods I mentioned?
                  are you saying they're broken?
                  what clarification do you need?
                  what ARE you saying
                  • 21. Re: Convert int[] to BufferedImage
                    807605
                    @SoulTech2012,

                    You seem to know what your talking about, but you
                    have yet to provide any code examples to clear up the
                    issue.
                    I've coded many applications involving swing, image processing, gaming, etc. I started doing this in 1997 so I think in the last 10 years there's been some code posted here and elsewhere. Learn to use Google or get a book. I'm not going to go look up code everytime someone needs help. Sorry.
                    Have you looked at the code from reply 5 of the link
                    I provided above? The only way I could get it to work
                    is to specify 3 separate ints for each pixel of
                    color.
                    Then change the code to work more efficieintly and use 1 single int.
                    Your above statement implies to me that I should be
                    able to combine all the color information for each
                    pixel into a single int. When I first wrote the code,
                    I tried that approach but could not get it to work.
                    That doesn't mean it's not possible!!! It just means you haven't figured it out. If you guys seriously can't get this to work I will post code within 24 hours.
                    • 22. Re: Convert int[] to BufferedImage
                      807605
                      Okay, I decided to do the search for you. Look at reply 9 to see an example of packing bytes into an int (i.e. convert color values to a pixel):

                      http://forum.java.sun.com/thread.jspa?threadID=520292&messageID=2494437
                      • 23. Re: Convert int[] to BufferedImage
                        800306
                        Hi all,

                        I've managed to solve the problem, mainly due to me gaining a better understanding of image processing/RGB stuff from this thread. My solution came from reply #15 and from the link that SoulTech posted.

                        @SoulTech: I understand that this whole problem seems easy to you, but it really was not for me. I have barely done anything with image processing. I searched these forums and Google for a long time and did not find much that made sense to me, which is why I posted a thread here; I figured the forum members here could clarify this more and direct me to relevant examples, which they did.

                        If I had more experience it would have been easier to see what methods to use in the API, or to know exactly what to search for and how to interpret it. However, I am still learning about all of this, so I believe that expecting me to know exactly what to do upon one quick search or one look at the API is somewhat unrealistic, unfortunately.

                        Your replies were all helpful though, and I really appreciate the assistance.

                        Thanks
                        • 24. Re: Convert int[] to BufferedImage
                          807605
                          Hi all,

                          I've managed to solve the problem, mainly due to me
                          gaining a better understanding of image
                          processing/RGB stuff from this thread. My solution
                          came from reply #15 and from the link that SoulTech
                          posted.

                          @SoulTech: I understand that this whole problem seems
                          easy to you, but it really was not for me. I have
                          barely done anything with image processing.
                          I'm truly sympathetic. But in this case you need to get a book or 2 on java image processing (although all the ones that i've read suck) and look at the code examples.
                          I searched these forums and Google for a long time and
                          did not find much that made sense to me, which is why
                          I posted a thread here; I figured the forum members
                          here could clarify this more and direct me to
                          relevant examples, which they did.

                          If I had more experience it would have been easier to
                          see what methods to use in the API, or to know
                          exactly what to search for and how to interpret it.
                          However, I am still learning about all of this, so I
                          believe that expecting me to know exactly what to do
                          upon one quick search or one look at the API is
                          somewhat unrealistic, unfortunately.

                          Your replies were all helpful though, and I really
                          appreciate the assistance.
                          I'm glad you got through it. Sorry if I'm an insensitive bastard sometimes. I agree that this particular issue is hard to search and find useful stuff on. Not many people do image processing with Java. More power to us!
                          • 25. Re: Convert int[] to BufferedImage
                            807605
                            this will convert the int array to a buffered image


                            public void saveImage(int[] pixels, String outfile, int w, int h) throws Exception
                                 {
                                      BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                                      // update the image with pixels[]
                                      newImage.setRGB(0, 0, w, h, pixels, 0, w);
                                      ImageIO.write(newImage, /*format*/"jpg", new File(outfile));
                                      System.out.println("Image Saved.");
                                      
                                 }
                            • 26. Re: Convert int[] to BufferedImage
                              camickr
                              Well, actually you can take the array directly, but it's ugly:
                              I agree its ugly and I have no idea what all those parameters for the BuffferedImage are.
                              Look at reply 9 to see an example of packing bytes into an int
                              Still have no idea what a MemoryImageSource is compared to a WritableRaster which I tried to use in my sample code. But at least I understand the packing code and can see how an array can be used directly.

                              Thanks for the tips.
                              1 2 Previous Next