JAVA: Image and Pixel Processing Part IV
Java
222
White

Nobody viết ngày 02/01/2022

Part IV

We are now able to control and to manipulate the pixels of an image. The logical operations like AND ( & ), OR ( | ), etc. allow us to mix, to merge images (plural) or to copy just a piece of image. Example: merge a piece of Image A into image B (logical OR operation)

    BufferedImage img_S = ImageIO.read(new File(source));
    int height = img_S.getHeight();
    int width  = img_S.getWidth();
    int wi = sourceX + xLen, hi = sourceY + yLen;
    //
    BufferedImage img_D = ImageIO.read(new File(dest));
    height = img_D.getHeight();
    width  = img_D.getWidth();
    //
    for (int y = destY, i = sourceY; y < height && i < hi; ++y, ++i) {
      for (int x = destX, j = ssourceX; x < width && j < wi; ++x, ++j) {
        int RGB = img_D.getRGB(x, y) | img_S.getRGB(j, i); // merge the two
        img_D.setRGB(x, y, RGB); // replace the original with the merged
      }
    }
    return img_D;

The outcome of the merge between FataMorgana.png and BigOnion.png
alt text

To overlay an image with another (piece of) image we simply replace the destination pixels with the source pixels:

img_D.setRGB(x, y, img_S.getRGB(j, i));

The OCR (Optical Character Recognition) technology is, for example, bases on the recognition of pixels and an algorithm that defines some certain pixel positions of each character. To do that every character has to be converted into an image. For example or alphanumeric letters. The letters are defined by a ''FONT** and its attributes (plain, bold, italic, etc.) The problem is how to create a Letter-Image. I show you hereunder a JAVA technique that allows you to convert any letter of any font into an image.

  public static BufferedImage createFontImage(String string, String fontName, int fontAtt, int size) {
    //  create a BufferImage with width = 1 and height = 1
    BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    // create a font with the given fontName (e.g. TimesRoman), attribute (e.g. Font.BOLD) and size (e.g. 15)
    Font font = new Font(fontName, fontAtt, size);
    // convert to FontMetrics
    FontMetrics metrics = g.getFontMetrics(font);
    int height = metrics.getHeight();
    int width  = metrics.stringWidth(string);
    // create an image with this width and height
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    // draw or write the given letter (or String) in BLACK with the background WHITE
    g = image.createGraphics();
    g.setFont(font);
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.BLACK);
    g.drawString(string, 0, height);
    //
    return image;
  }

You may wonder why I have to convert the Font into FontMetrics, right?
Font metrics are the measurements of characters in a particular font, which allow you to evenly space and uniformly align lines of text...
(more about Font metrics: click "HERE") . I have to do that in order to get the Width and the Height of the given font. Example: letter** H, TimesRoman, BOLD, 40**
alt text
We got the Width 31 and the Height 52 (total: 6448 Pixels). But, as you have already noticed, the upper part of the image is "empty" (or void with WHITE) and that falsifies the true height of the letter H with Font TimesRoman, BOLD and 40 dpi (Dot Per Inch). So. The next step is to refine the H image in order to achieve the true Height of this letter.

    ...
    g = image.createGraphics();
    g.setFont(font);
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.BLACK);
    g.drawString(string, 0, height);
    //
    // eliminate the "void"
    //
    int y = 0;
    int white = Color.WHITE.getRGB();
    LOOP: for (; y < height; ++y)
    for (int x = 0; x < width; ++x)
    if (image.getRGB(x, y) != white) {
      if (y == 0) return image; // no need to rectify
      break LOOP;
    }
    int H = height - y;
    int[] pixels  = image.getRGB(0, y, width, H, null, 0, width);
    BufferedImage img = new BufferedImage(width, H, BufferedImage.TYPE_INT_ARGB);
    img.setRGB(0, 0, width, H, pixels, 0, width);
    return img;

And the result with less pixels than the original.
alt text
Hope that you have enjoyed my tutorial about Image and Pixel Processing

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

White

Nobody

7 bài viết.
482 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Bài viết liên quan
White
2 0
Trong bài viết này, một số hình ảnh hoặc nọi dung có thể bị thiếu do quá trình chế bản. Vui lòng xem nội dung ở blog gốc sau: (Link) (Link), chúng...
programmerit viết hơn 6 năm trước
2 0
White
0 0
Giới thiệu Trong bài hôm nay chúng ta sẽ tìm hiểu cách handle request POST của Spring Boot. Trước đó, bạn nên biết 1. 「Spring Boot 8」Tạo Web He...
https://loda.me viết hơn 2 năm trước
0 0
Male avatar
0 0
https://grokonez.com/deployment/vultr/howtoinstalljavainubunturemoteservervutrhostingvpsexample How to install Java on Ubuntu Remote Server – Vutr...
loveprogramming viết 9 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


White
{{userFollowed ? 'Following' : 'Follow'}}
7 bài viết.
482 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!