2014-02-14 268 views
0

我还在学习Android编程,现在我正在编写一个应用程序来检测从画廊上传的图片中的颜色。到目前为止,使用教程,应用程序可以将图片从图库上传到ima​​geview,但我不知道如何让第二部分工作。我希望用户能够简单地按下按钮并显示图像的颜色(现在我正在使用单色图像)。我会如何去做这件事?下面是我的源代码至今:使用Android中的按钮获取图像的像素颜色?

package com.example.testrunvday; 

import com.example.testrunvday.R; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 

    int test = 0; //will give 1 if red is detected, 0 if not 

    private static final int SELECT_PICTURE = 1; 

    private String selectedImagePath; 
    private ImageView img; 
    private Button pixel; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     img = (ImageView)findViewById(R.id.ImageView01); 

     ((Button) findViewById(R.id.Button01)) 
       .setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
         Intent intent = new Intent(); 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE); 
        } 
       }); 

     pixel = (Button) findViewById(R.id.pixel); 
     pixel.setOnClickListener(new Button.OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 


       //so far everything I've written here hasn't worked) 



      } 







     }); 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE) { 
       Uri selectedImageUri = data.getData(); 
       selectedImagePath = getPath(selectedImageUri); 
       System.out.println("Image Path : " + selectedImagePath); 
       img.setImageURI(selectedImageUri); 
      } 
     } 
    } 

    public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<Button 
    android:id="@+id/pixel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="pixel" /> 

<Button 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Browse gallery" /> 

<ImageView android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</ImageView> 
</LinearLayout> 

回答

0

获取的ImageView位:

int pixel = bitmap.getPixel(x,y);

:在位置像素的

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

取色

劈RGB:

int redValue = Color.red(pixel); 
int blueValue = Color.blue(pixel); 
int greenValue = Color.green(pixel); 

有你有你的一个特定的像素颜色。

0
ImageView imageView = (ImageView)findViewById(R.id.ImageView01); 
Bitmap bitmapImageView = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); 
int pixel = bitmapImageView.getPixel(x,y);