在android中的图像视图中显示矩阵

时间:2015-01-31 08:20:11

标签: android

我正在android中进行一些图像处理,但我在以下代码中遇到了初始问题。

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);

}  

我想在ImageView(或任何其他视图)中将rgbValues矩阵显示为图像,但我收到了错误:

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

如何将rgbValues矩阵显示为图像?

2 个答案:

答案 0 :(得分:2)

ImageView.setImageMatrix()用于设置图像的平移,旋转和缩放矩阵。它不是用于设置RGB值。要设置RGB值,您应使用Bitmap.setPixels()Bitmap.createBitmap()。此外,您需要将RGB值放入一维数组中,而不是二维数组中。不要问我为什么,它是Android API(它可能是出于速度原因)。

因此,更改代码以写入1D数组:

private int[] rgbValues;

使用数组[(y * width)+ x]而不是数组[y] [x]或数组[x] [y]来处理1D数组,就像它是2D数组一样:

//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);

    }  
} 

然后从这个数组创建一个Bitmap并设置为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);

答案 1 :(得分:0)

尝试使用此代码从资源中设置图像..

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;

}