问题
I'm still learning Android programming, and right now I'm writing an app to detect colors in a picture uploaded from the gallery. So far, using a tutorial, the app can upload a picture from the gallery into imageview, but I'm not sure how to get the second part working. I want the user to be able to simply press a button and display the color of the image (right now I'm using single color images). How would I go about doing this? Here's my source code so far:
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>
回答1:
get imageview's bitmap:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
get color of pixel at location:
int pixel = bitmap.getPixel(x,y);
split in RGB:
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
there you have your color for a specific pixel.
回答2:
ImageView imageView = (ImageView)findViewById(R.id.ImageView01);
Bitmap bitmapImageView = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmapImageView.getPixel(x,y);
来源:https://stackoverflow.com/questions/21779371/getting-the-pixel-color-of-image-using-a-button-in-android