1

我正在编写一个程序,它将获取二维码图像并识别黑白像素并将它们保存在一个数组中。但我的问题是;我有 294x294 像素图像和一个固定大小为 21x21 的矩阵。所以一个方形模块对我来说是 294/21=4.0 像素。如何按 4.0 像素到 4.0 像素搜索像素?这是我的代码:

public static void printAllARGBDetails(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        System.out.println("Image Dimension: Height-" + height + ", Width-"
                + width);
        System.out.println("Total Pixels: " + (height * width));
        int pixels[][]=new int[21][21];
        int pixel=width/21;
        System.out.println("Pixel=" +pixel);

        for (int i = 20; i>=0 ; i--) {
            for (int j = 20; j>=0 ; j--) {
                if(image.getRGB(i, j)==-1)
                {

                    pixels[i][j]=0;
                }
                else
                {
                    pixels[i][j]=1;
                }

            }
        } 
  }

`

4

2 回答 2

0

下面编辑的代码应该可以工作,但它只测试每个元素中 16 个像素中的 1 个

    for (int i = 0; i<20 ; i++) {
        for (int j = 0; j<20 ; j++) {
            if(image.getRGB(i*4, j*4)==-1)
            {

                pixels[i][j]=0;
            }
            else
            {
                pixels[i][j]=1;
            }

        }
    } 
于 2012-11-14T17:02:43.090 回答
0

您可能应该引入一种方法,该方法以某种方式将 4x4 像素阵列评估为0or 1,例如基于平均亮度:

public static void printAllARGBDetails(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();
    System.out.println("Image Dimension: Height-" + height + ", Width-"
            + width);
    System.out.println("Total Pixels: " + (height * width));
    int pixels[][]=new int[21][21];
    int pixel=width/21;
    System.out.println("Pixel=" +pixel);

    for (int i = 20; i>=0 ; i--) {
        for (int j = 20; j>=0 ; j--) {
                pixels[i][j] = getPixel (image, i, j);
                System.out.println(String.format ("Pixel[%d,%d]=%d", i, j, pixels[i][j]));
        }
    } 
}

private static final long LUMINOSITY_THRESHOLD = 23000;

private static int getPixel (BufferedImage image, int i, int j) {
   long totalLuminosity = 0;
   for (int k = 0; k < 4; k++) {
      for (int m = 0; m < 4; m++) {
          long luminosity = getLuminosity (image.getRGB (i*4 + k, j*4 + m));
          totalLuminosity += luminosity;
      }
   }

   return totalLuminosity/16 > LUMINOSITY_THRESHOLD ? 0 : 1;
}

private static int getLuminosity (int rgb) {
    int r = rgb >> 16;
    int g = (rgb >> 8) & 0xFF;
    int b = rgb & 0xFF;

    int luminosity = 21*r + 71*g + 7*b;

    return luminosity;
}    
于 2012-11-14T17:03:05.033 回答