Drawing image border using Java Code

 





In our last tutorial we have seen how to re-size image using Java code. In this tutorial we will see how to draw a border in the image using Java code.



import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageBorder {

    public static void main(String[] args) {
        new ImageBorder().createBorderImage("e:\\tree.jpg", "e:\\tree_border.jpg");
    }
    
    public void createBorderImage(String oriImgUrl, String saveLocFilePath){
        try {
            int borderColor = 0x99FF0000; //Red color
            BufferedImage image = ImageIO.read(new File(oriImgUrl));
            for(int i=0;i < image.setRGB(0, i, borderColor);
                image.setRGB(image.getWidth()-1, i, borderColor);
            }
            for(int i=0;i < image.setRGB(i, 0, borderColor);
                image.setRGB(i, image.getHeight()-1, borderColor);
            }
            ImageIO.write(image, "png"new File(saveLocFilePath));
            
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}






No comments:
Write comments