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

}  

我想将rgbValues矩阵显示为ImageView(或任何其他视图)中的图像,但是出现了这个错误:

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

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

萨格马克

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

因此,更改代码以写入一维数组:

private int[] rgbValues;

使用array [(y * width)+ x]代替array [y] [x]或array [x] [y]来寻址一维数组,就好像它是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);

    }  
} 

然后从此数组创建一个位图并设置为您的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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章