I am trying to implement this code:
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
}
}
}
I can’t seem to figure out what this line of code does
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.four_colors);
when i try to implement it, eclipse will say it can’t find what four_colors is, i’ve got no idea what it is and cant seem to figure it out.
Do you guys know what it is? and how should it be used?
thank in advance
R is an automatically generated file that keeps track of the resources in your project. The drawable means the resource is of the type drawable, usually (but not always) meaning the resource is in one of your
res/drawables-folders, e.g.res/drawables_xhdpi. Four_colors refers to the resource name, usually indicating the file you’re referring to is a file called ‘four_colors` (e.g. a PNG-file) in e.g. the res/drawables-xhdpi folder.So, the four_colors refers to the name of the (in this case) drawable your app trying to load.
When Eclipse says it cannot find the resource, it means the resource is not included in the project where it should be included. E.g. you copied some code, but not the drawables that are referred to in the code.
The line
BitmapFactory.decodeResource(...)does exactly what it says; it decodes the encoded image into a bitmap, something Android can actually show. Usually when you use bitmaps it does this kind of decoding under the hood; here it’s done manually.