如何在android中读取图像

时间:2013-07-30 12:48:32

标签: android image pixel rgb

目标:读取图像并获取其RGB像素值。

问题:事实是我在eclipse中的项目(测试)下创建了一个新文件夹(img)并粘贴了一个.jpg文件(test.jpg)并试图用这段代码读取它:

tig = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+“/ testing / img / test.jpg”);             AlertDialog.Builder bld = new AlertDialog.Builder(this);             bld.setTitle( “欢呼”);             int pxl = tig.getPixel(30,40);             bld.setMessage(PXL);             bld.show(); 但在运行时应用程序习惯“不幸关闭”,然后我尝试将图像放在资产文件夹和解码流中,没有用。

问题 1.是不是可以在项目中创建我自己的文件夹并在那里放置文件 2.或者我应该在哪里放置图像来读取像素。

我在这里需要真正的勺子喂养,因为它是Android开发的新手。

提前致谢。

1 个答案:

答案 0 :(得分:0)

根据我的理解,我猜这是你需要的

更改

Bitmap src = (BitmapDrawable) this.getResources().getDrawable(R.drawable.yourimagename);

Bitmap src = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.yourimagename);

再次检查

Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
for(int x = 0;x < bitmap.getWidth();x++)
    for(int y = 0;y < bitmap.getHeight();y++)
    {
   // here bitmap.getPixel(x, y)) gets the pixel at the position (x,y)  
   int pixelvalue = bitmap.getPixel(x, y));
    int rValue = color.red(pixelvalue); // gives value of R in "RGB"
    int gValue = color.green(pixelvalue); // gives value of G in "RGB"
    int bValue = color.blue(pixelvalue); // gives value of B in "RGB"
}

将您的drawable放在这里

enter image description here

快乐编码