Results 1 to 6 of 6

Thread: Convert a table BufferedImage

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Convert a table BufferedImage

    Hello,
    I would like to know how to convert a table with each cell contains the value of a pixel between 0 to 255 in a BufferedImage?

    I have tried the following code, but it does not work
    Code:
    bufedImage buf = new bufedImage(width, height, bufedImage.TYPE_BYTE_GRAY) ;
    buf.setRGB(0, 0, Width, height, pixels, 0, Width) ;
    If you have a solution for this then please do post back I am the interested on here. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Convert a table BufferedImage

    Hello,
    Have you tries with setPixel void (int x, int y, int [] t) in the case of an image of gray. t contains only 1 row and one column contention the pixel value. x and y respectively indicates the line you column of pixels. I think you should try with the following, this will definitely give you an idea about it. If you need any more help regarding this topic then do post back and we will try to help you.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Convert a table BufferedImage

    Hello,
    Have a look at the following code, please see if this works for you
    Code:
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    int c = 0;
    for( int i = 0; I <width; i + +)
    {
       for(int j = 0J <height; j + +){
         picture.getRaster().setPixel(i, j, new int[1]{c});
    }
    c + +;
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Convert a table BufferedImage

    Hello,
    Besides the proposed solution, what does not work exactly? Did you try to convert your picture to gray pixels in RGB pixel array before calling the method?
    Code:
    int[] colors = new int[pixels.length];
    for (int i = 0 ; I <pixels.length ; I + +) {
      colors[i] = ((pixels[i] & 0xFF) << 16) | ((pixels[i] & 0xFF) << 8) | ((pixels[i] & 0xFF) << 0);
    }
    buffer.setRGB(0, 0, Width, height, colors, 0, Width) ;
    Obviously, this way, It will use more memory than the method suggested and is probably not preferable. Note, however, direct access to Raster a BufferedImage removes any potential graphics accelaration.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Convert a table BufferedImage

    Hello,
    This code is going to help you, take a look at it
    Code:
    public class buffimgConverter {
    
      // default version of crbufimg
      static public buffimg crbufimg(Image imageIn,
          Component comp) {
        return crbufimg(imageIn, buffimg.TYPE_INT_ARGB, comp);
      }
    
      static public buffimg crbufimg(Image imageIn,
          int imageType, Component comp) {
        MediaTracker med = new MediaTracker(comp);
        med.addImage(imageIn, 0);
        try {
          med.waitForID(0);
        } catch (InterruptedException ie) {
        }
        buffimg buffimgo = new buffimg(imageIn
            .getWidth(null), imageIn.getHeight(null), imageType);
        Graphics g = buffimgo.getGraphics();
        g.drawImage(imageIn, 0, 0, null);
    
        return buffimgo;
      }
    }

  6. #6
    Join Date
    Dec 2009
    Posts
    211

    Re: Convert a table BufferedImage

    Hello,
    I am new to the swing components in java, but you can try the following
    Code:
    javax.swing.JFrame f;                  // Initialized elsewhere
    
    // Create an image, and wait for it to load
    Image img = javax.swing.ImageIcon("myimage.gif").getImage();
    
    // Create a BufferedImage of the same size as the Image
    BufferedImage buff = (BufferedImage)f.createImage(img.getWidth(f),img.getHeight(f));
    
    
    
    Graphics2D g = buff.createGraphics();    // Get a Graphics2D object
    g.drawImage(img, 0, 0, f);      // Draw the Image data into the BufferedImage

Similar Threads

  1. Replies: 5
    Last Post: 27-08-2011, 10:53 AM
  2. Link a Table to another Table to Drop Down In Main Table
    By himeshRES in forum Windows Software
    Replies: 6
    Last Post: 11-12-2010, 01:01 PM
  3. To convert a pivot table to a flattened table in MS Excel
    By zeemga in forum Windows Software
    Replies: 3
    Last Post: 27-11-2010, 05:48 AM
  4. How can I convert text data into table format?
    By Ram Bharose in forum Windows Software
    Replies: 7
    Last Post: 30-12-2009, 01:58 PM
  5. Convert Excel data to HTML Table
    By Chalina in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 11:21 PM

Posting Permissions

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