2012-03-29 113 views
0

我得到一个小问题,我得到一个代码,它读取所有像素并获取像素的RGB颜色。但在此之前,我将图片分块,所以我也可以计算出更大的图像,而不会超过内存。 这里是代码:获取特定像素的x,y位置

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 

      try { 
       decoder_image = BitmapRegionDecoder.newInstance(filePath, 
         false); 


      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       boolean bool_pixel = true; 
       final int width = decoder_image.getWidth(); 
       final int height = decoder_image.getHeight(); 
       // Divide the bitmap into 1100x1100 sized chunks and process it. 
       // This makes sure that the app will not be "overloaded" 
       int wSteps = (int) Math.ceil(width/1100.0); 
       int hSteps = (int) Math.ceil(height/1100.0); 
       Rect rect = new Rect(); 
       for (int h = 0; h < hSteps; h++) { 
        for (int w = 0; w < wSteps; w++) { 
         int w2 = Math.min(width, (w + 1) * 1100); 
         int h2 = Math.min(height, (h + 1) * 1100); 
         rect.set(w * 1100, h * 1100, w2, h2); 
         bitmap_image = decoder_image.decodeRegion(rect, 
           null); 

         try { 
          int bWidth = bitmap_image.getWidth(); 
          int bHeight = bitmap_image.getHeight(); 
          int[] pixels = new int[bWidth * bHeight]; 
          bitmap_image.getPixels(pixels, 0, bWidth, 0, 0, 
            bWidth, bHeight); 
          for (int y = 0; y < bHeight; y++) { 
           for (int x = 0; x < bWidth; x++) { 

            int index = y * bWidth + x; 
            int R = (pixels[index] >> 16) & 0xff; //bitwise shifting 
            int G = (pixels[index] >> 8) & 0xff; 
            int B = pixels[index] & 0xff; 
            total++; 

            if (R == 255){ 
            //Save x,y position          
            } 
            if ((G > (R+2)) && (G > (B+2))) { 
            counter++; 
            } 
           } 
          } 
         } finally { 
          bitmap_image.recycle(); 
         } 
        } 
       } 
      } finally { 
       decoder_image.recycle();      
      } 

这就像一个魅力。有来自互联网的一些信息,我重拍这个为我自己的代码。

但我现在想要什么,即:当他检测到一个“全”红色像素(255)时,他必须显示该像素的x,y位置。

我认为这很容易做到,但因为我把图像切成卡盘,我得到了很奇怪的x,y位置(我认为)。

我会快速解释为什么我要这样做:在图像中,它们是2个红色像素,而这2个像素必须转换成矩形,但我没有得到好的x,y位置。

因此,要明确问题:如何获取红色像素的x,y位置。

也许很容易,但不知何故,我只是没有得到正确的位置,甚至没有接近。我在Photoshop中打开图像来检查它,看看红色像素的x,y是什么,但是我的应用程序给出了非常奇怪的坐标。

如果您需要更多信息或其他内容,请发表评论。

感谢不已, Bigflow

+0

因此,您需要创建一个计算像素位置的算法? – Th0rndike 2012-03-29 13:17:52

+0

杰克的回答是正确的 – Bigflow 2012-03-29 14:33:06

回答

1

COORDS 应该被rect.left + x和rect.top + Y。你已经尝试过了吗?

+0

工作完美,谢谢。 – Bigflow 2012-03-29 14:32:46

0

x和y是图像中水平和垂直像素位置的自身全局坐标,取决于您尊重的坐标系的原点以及图像如何根据它移动。