5 Replies Latest reply: Apr 19, 2009 2:23 PM by 843799 RSS

    BufferedImage border - help

    843799
      I have a BufferedImage and I want to create a 1 pixel white border around it.

      I think I should be able to place my BuffereImage on to another BuffereImage but I am struggling to do this. can anyone point me in the right direction?

      Thanks,
        • 1. Re: BufferedImage border - help
          800313
          I dont really understand why you want to place an image on another image? see this example, is this what you are looking for?
          package tests;
          
          import java.awt.*;
          import javax.swing.*;
          import java.awt.image.BufferedImage;
          import javax.imageio.ImageIO;
          import java.io.*;
          import javax.swing.BorderFactory; 
          
          class testImagePanel{
              public static void main(String[] args){
                  
                  BufferedImage image = null;
                  ImagePanel imagePanel = null;
                  try{
                      image = ImageIO.read(new File("Pictures/pl.jpg"));
                      imagePanel = new ImagePanel(image);
                  }catch(IOException  e){
                       System.err.println("Trying to read in image "+e);
                  }
                  
                  JFrame frame = new JFrame("Example");
                  frame.add(imagePanel);
                  frame.pack();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);                
                  
          
              }
          }
          
          public class ImagePanel extends JPanel {
              BufferedImage image;
              Dimension size;
          
              public ImagePanel(BufferedImage image) {
                  this.image = image;
                  this.size = new Dimension();
                  size.setSize(image.getWidth(), image.getHeight());
                  this.setBackground(Color.WHITE);
                  this.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
              }
          
              @Override
              protected void paintComponent(Graphics g) {
                  // Center image in this component.
                  int x = (getWidth() - size.width)/2;
                  int y = (getHeight() - size.height)/2;
                  g.drawImage(image, x, y, this);
              }
              @Override
              public Dimension getPreferredSize() { return size; }
              
          }
          Calypso
          • 2. Re: BufferedImage border - help
            843799
            Thanks for the response. Its not really what I wanted to do.

            I want to read in a .bmp file and put a 1 pixel white border around the image then save it again, so if I open the image in any image viewer and zoom in, I will see the exact same image but with a 1 pixel white border around it.

            Cheers
            • 3. Re: BufferedImage border - help
              843799
              1) Create a BufferedImage 2 pixels taller and 2 pixels wider then your source image.
              2) Fill this newly created BufferedImage with white
              Graphics g = newImage.getGraphics();
              g.setColor(Color.white);
              g.fillRect(0,0,newImage.getWidth(),newImage.getHeight());
              3) Draw the old image onto the new one starting at (1,1)
              g.drawImage(oldImage,1,1,null);
              4) Save the new image.
              ImageIO.write(...);
              • 4. Re: BufferedImage border - help
                843799
                This is what I have, but it creates a blank image. Whats is going wrong?
                        BufferedImage border = new BufferedImage(bi.getWidth()+2,bi.getHeight()+2,BufferedImage.TYPE_INT_ARGB );
                        
                        Graphics graphic = border.getGraphics();  
                        graphic.setColor(Color.white);
                        graphic.fillRect(0,0,border.getWidth(),border.getHeight());
                        graphic.drawImage(bi,1,1,null);
                        
                        
                       try{
                        File file = new File("newImage.bmp");
                        ImageIO.write(border,"bmp",file);//write new file out
                       }
                       catch(Exception o){o.printStackTrace();}
                Cheers
                • 5. Re: BufferedImage border - help
                  843799
                  The bmp ImageWriter probably doesn't like your TYPE_INT_ARGB image. It saves in bmp version 3, which dosen't support alpha values. So change it to TYPE_INT_RGB.