我还在学习Android编程,现在我正在编写一个应用来检测从图库上传的图片中的颜色。到目前为止,使用教程,应用程序可以将图片从库中上传到imageview,但我不知道如何使第二部分工作。我希望用户能够简单地按下按钮并显示图像的颜色(现在我正在使用单色图像)。我该怎么做呢?这是我到目前为止的源代码:
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 :(得分:0)
获取imageview的位图:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
获取位置处的像素颜色:
int pixel = bitmap.getPixel(x,y);
以RGB分割:
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
你有特定像素的颜色。
答案 1 :(得分:0)
ImageView imageView = (ImageView)findViewById(R.id.ImageView01);
Bitmap bitmapImageView = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmapImageView.getPixel(x,y);