Java Utililty Methods BufferedImage Operation

List of utility methods to do BufferedImage Operation

Description

The list of methods to do BufferedImage Operation are organized into topic(s).

Method

BufferedImagemakeOpaque(BufferedImage img, Color col)
make Opaque
return convertImage(img, BufferedImage.TYPE_BYTE_INDEXED, col);
PolygonMakePoly(BufferedImage spr, int d, int angle, int baseX, int baseY)
Create a polygon based on a image.
Polygon p = new Polygon();
int w = spr.getWidth(null);
int h = spr.getHeight(null);
try {
    int[] vertex_x = new int[255], vertex_y = new int[255], vertex_k = new int[255];
    int numPoints = 0, tx = 0, ty = 0, fy = -1, lx = 0, ly = 0;
    vertex_x[0] = 0;
    vertex_y[0] = 0;
...
BufferedImagemakeTintedCopy(BufferedImage bi, Color tint)
make Tinted Copy
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
        .getDefaultConfiguration();
BufferedImage tinted = gc.createCompatibleImage(bi.getWidth(), bi.getHeight(), Transparency.TRANSLUCENT);
for (int y = 0; y < bi.getHeight(); y++) {
    for (int x = 0; x < bi.getWidth(); x++) {
        int rgb = bi.getRGB(x, y);
        if (rgb != 0)
            tinted.setRGB(x, y, tint.getRGB());
...
BufferedImagemarkImageBorders(final BufferedImage img, final int startW, final int startH)
Fully marks the bottom and right borders of an image transparent (transparent white).
final BufferedImage copy = copyImage(img);
final Color markTransparentWhite = new Color(255, 255, 255, 0);
final Graphics2D g = copy.createGraphics();
g.setColor(markTransparentWhite);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f));
if (startW < copy.getWidth()) {
    g.fillRect(startW, 0, copy.getWidth() - startW, copy.getHeight());
if (startH < copy.getHeight()) {
    g.fillRect(0, startH, copy.getWidth(), copy.getHeight() - startH);
g.dispose();
return copy;
BufferedImagematBytesToBufferedImage(byte[] data, int cols, int rows, int type)
mat Bytes To Buffered Image
int bufferedType = 10;
switch (type) {
case 0:
    bufferedType = BufferedImage.TYPE_BYTE_GRAY;
    break;
case 16:
    bufferedType = BufferedImage.TYPE_3BYTE_BGR;
    break;
...
BufferedImagematte(BufferedImage image, int top, int bottom, int left, int right, Color bg)
Add a matte border around the image
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
BufferedImage newImage = new BufferedImage(imageWidth + left + right, imageHeight + top + bottom,
        getImageType(image));
Graphics newG = newImage.getGraphics();
newG.setColor(bg);
newG.fillRect(0, 0, newImage.getWidth(null), newImage.getHeight(null));
newG.drawImage(image, left, top, null);
...
BufferedImagemirror(final BufferedImage image, final boolean horizontal)
mirror
int width = image.getWidth();
int height = image.getHeight();
BufferedImage output = new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
for (int i = 0; i < width; i++) {
    for (int j = 0; j < image.getHeight(); j++) {
        if (horizontal) {
            output.setRGB(width - i - 1, j, image.getRGB(i, j));
        } else {
...
BufferedImagenewOptimizedImageLike(GraphicsConfiguration destination, BufferedImage img)
new Optimized Image Like
return newOptimizedImage(destination, img.getWidth(), img.getHeight(), img.getColorModel().hasAlpha(),
        img.getTransparency());
BufferedImagenewSubimage(BufferedImage src, int x, int y, int w, int h)
Returns an independent subimage of the given main image.
BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
int[] tmp = new int[w * h];
src.getRGB(x, y, w, h, tmp, 0, w);
bimg.setRGB(0, 0, w, h, tmp, 0, w);
return bimg;
intnNeighbors(BufferedImage image, int i, int j)
n Neighbors
int res = 0;
int col = image.getRGB(i, j);
for (int di = -1; di <= 1; di++)
    for (int dj = -1; dj <= 1; dj++)
        if (di != 0 || dj != 0) {
            int x = i + di, y = j + dj;
            if (x < 0 || x >= image.getWidth() || y < 0 || y >= image.getHeight())
                continue;
...