2012-09-16 17 views
0

我想實現這個代碼:BitmapFactory.decodeResource(getResources(),R.drawable.four_colors); four_colors是什麼意思?

package fortyonepost.com.iapa; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 

public class ImageAsPixelArray extends Activity 
{ 
//a Bitmap that will act as a handle to the image 
private Bitmap bmp; 

//an integer array that will store ARGB pixel values 
private int[][] rgbValues; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //load the image and use the bmp object to access it 
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.four_colors); 

    //define the array size 
    rgbValues = new int[bmp.getWidth()][bmp.getHeight()]; 

    //Print in LogCat's console each of one the RGB and alpha values from the 4 corners of the image 
    //Top Left 
    Log.i("Pixel Value", "Top Left pixel: " + Integer.toHexString(bmp.getPixel(0, 0))); 
    //Top Right 
    Log.i("Pixel Value", "Top Right pixel: " + Integer.toHexString(bmp.getPixel(31, 0))); 
    //Bottom Left 
    Log.i("Pixel Value", "Bottom Left pixel: " + Integer.toHexString(bmp.getPixel(0, 31))); 
    //Bottom Right 
    Log.i("Pixel Value", "Bottom Right pixel: " + Integer.toHexString(bmp.getPixel(31, 31))); 

    //get the ARGB value from each pixel of the image and store it into the array 
    for(int i=0; i < bmp.getWidth(); i++) 
    { 
     for(int j=0; j < bmp.getHeight(); j++) 
     { 
      //This is a great opportunity to filter the ARGB values 
      rgbValues[i][j] = bmp.getPixel(i, j); 
     } 
    } 

    //Do something with the ARGB value array 
} 
} 

} 

我似乎無法找出這行代碼確實 BMP = BitmapFactory.decodeResource(getResources(),R.drawable.four_colors);
當我嘗試實現它,日食會說找不到four_colors是什麼,但我沒有任何知道它是什麼和不能似乎弄明白。 你們知道它是什麼嗎?它應該如何使用? 提前致謝

回答

5

R是一個自動生成的文件,用於跟蹤項目中的資源。提拉意味着資源的類型是可繪製的,通常(但並不總是)意味着資源在你res/drawables -folders,例如之一res/drawables_xhdpi。 Four_colors指的是資源名稱,通常表示你所指的文件是一個名爲'four_colors'(例如PNG文件)的文件。 res/drawables-xhdpi文件夾。

因此,four_colors引用您的應用程序嘗試加載的(在本例中)drawable的名稱。

當Eclipse說找不到資源時,這意味着該資源不包含在它應該包含的項目中。例如。您複製了一些代碼,但不是代碼中引用的可繪製的代碼。

該行BitmapFactory.decodeResource(...)確實如它所說;它將編碼的圖像解碼爲位圖,這是Android實際可以顯示的內容。通常當你使用位圖的時候,它會在底層進行這種解碼;這裏是手動完成的。

+0

確定,所以它實際上是照片即時通訊試圖analize?那麼如果它應該分析我剛剛拍攝的照片,我應該通過照片的路徑?我怎樣才能做到這一點?非常感謝! – oskar132

+0

您需要使用BitmapFactory上的其他方法之一,如decodeFile。資源已內置到您的應用程序中。 –

1

您需要下載this圖片並將其放入您的./res/drawable文件夾中。確保右鍵單擊該項目並選擇刷新。

+1

請注意,這只是爲了解決問題。我建議閱讀Reinier的答案,找出實際發生的事情。 – Jerzmacow

1

four_colors是圖像的名稱。你必須把在清晰度圖片/繪製的圖像應該有名稱four_colors.jpg