farhan
farhan

Reputation: 78

DIsplaying matrix in image view in android

I am doing some image processing in android but I got an initial problem in the following code.

public class MainActivity extends ActionBarActivity {

private Bitmap bmp; 
private int[][] rgbValues,redv,redg,redb;
public int[] nn ; 
int values,val, c;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //load the image and use the bmp object to access it  
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.capture);  

    //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);  
             values = rgbValues[i][j];
             int rvalue = Color.red(values);
             Log.i("Pixel Value", "Red pixel: " + rvalue);
             int gvalue = Color.green(values);
             Log.i("Pixel Value", "Green pixel: " + gvalue);
             int bvalue = Color.blue(values);
             Log.i("Pixel Value", "Blue pixel " + bvalue);

        }  
    } 
    ImageView miamageview ;

    miamageview = ( ImageView) findViewById (R.id.imageView1);
    miamageview.setImageMatrix(rgbValues);

}  

I want to display the rgbValues matrix as an image in ImageView (or any other view) but I got that error :

"The method setImageMatrix(Matrix) in the type ImageView is not applicable for the arguments `(int[][])`". 

How can i display rgbValues matrix as an image?

Upvotes: 4

Views: 3336

Answers (2)

samgak
samgak

Reputation: 24427

ImageView.setImageMatrix() is for setting the translation, rotation and scaling matrix of an image. It's not for setting RGB values. To set RGB values, you should use Bitmap.setPixels() or Bitmap.createBitmap(). Also, you need to put your RGB values into a 1-dimensional array, not a 2-dimensional one. Don't ask me why, it's the Android API (it's probably for speed reasons).

So, change your code to write to a 1D array:

private int[] rgbValues;

Use array[(y * width) + x] instead of array[y][x] or array[x][y] to address a 1D array as if it was a 2D array:

//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[(j * bmp.getWidth()) + i] = bmp.getPixel(i, j);  
         values = rgbValues[(j * bmp.getWidth()) + i];
         int rvalue = Color.red(values);
         Log.i("Pixel Value", "Red pixel: " + rvalue);
         int gvalue = Color.green(values);
         Log.i("Pixel Value", "Green pixel: " + gvalue);
         int bvalue = Color.blue(values);
         Log.i("Pixel Value", "Blue pixel " + bvalue);

    }  
} 

then create a Bitmap from this array and set to your ImageView:

ImageView miamageview ;

miamageview = ( ImageView) findViewById (R.id.imageView1);

Bitmap bitmap = Bitmap.createBitmap(rgbValues, bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
miamageview.setImageBitmap(bitmap);

Upvotes: 2

Amitsharma
Amitsharma

Reputation: 1576

Try with this code to set image from resource..

public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable)drawable).getBitmap();
}

int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); 
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;

}

Upvotes: 0

Related Questions