2014-04-17 40 views
0

我有一个jpg图像。首先,我想将其转换为位图,以便我可以从我的照片中获取所有像素。 然后,我想保持像素成为一个矩阵的行和列的二维数组。 然后,我想要搜索行或列或两者以匹配一种颜色。这意味着我想要查找该2D矩阵的某一行(或任何我想要的列)是否包含红色(例如)。我想是这样的:在android中使用位图图像

  Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.laser); 
     bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);   

     int[] pixels = new int[bmp.getHeight()*bmp.getWidth()]; 
     bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight()); 

      for(int i =0; i<pixels.length;i++){  
      if(pixels[i]==0xffff0000) 
       Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",     Toast.LENGTH_LONG).show(); 
       } 

在这里,我把像素到一个数组(1D),并检查是否有红色的存在,或者不在该数组。 但它没有找到,虽然我的照片中存在红色。 这里可能是阵列不能获取像素。 但这不是我想要的。我想上面提到的。我怎样才能达到我的目标?

+1

OpenCV?)........ –

+0

我想要它在Android手机 – DarkenShooter

+0

OpenCV已移植到Android。您可以在Google Play商店中安装名为OpenCV Manager的应用程序库。 OpenCV的Java接口可以打包在.apklib中,以便IDE可以找到它们。 –

回答

0

如果您有位图对象,则可以使用Bitmap::getPixels方法获取像素。 例如,在这里我得到的图像的第一列:

// byte[] data2 contains image binary date from the network 
    Bitmap bitmap = BitmapFactory.decodeByteArray(data2, 0, data2.length); 

    int[] pixels = new int[bitmap.getHeight()]; 

    int offset = 0; 
    int stride = 1; 
    int x = 0; 
    int y = 0; 
    int width = 1; 
    int height = bitmap.getHeight(); 

    bitmap.getPixels(pixels, offset, stride, x, y, width, height); 

每个像素只是一个Color对象,所以你可以检查它的RGB值。

+0

你能告诉我如何比较我想要的RGB值与行RGB值? @pelotasplus – DarkenShooter

+0

遍历行/列并检查值?假设你想匹配只有红色像素的列/行,那么你的代码可能看起来像这样: 'for(int i = 0; i pelotasplus

+0

是我提到的方法来将正常图像转换为位图正确? @pelotasplus – DarkenShooter