使用Android中的按钮获取图像的像素颜色? [英] Getting the Pixel Color of Image using a Button in Android?
本文介绍了使用Android中的按钮获取图像的像素颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我仍在学习Android编程,现在我正在编写一个应用程序,以检测从图库上传的图片中的颜色.到目前为止,使用教程,该应用程序可以将图库中的图片上传到imageview中,但是我不确定如何使第二部分正常工作.我希望用户能够简单地按一个按钮并显示图像的颜色(现在我正在使用单色图像).我将如何去做呢?到目前为止,这是我的源代码:
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>
推荐答案
ImageView imageView = (ImageView)findViewById(R.id.ImageView01);
Bitmap bitmapImageView = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmapImageView.getPixel(x,y);
这篇关于使用Android中的按钮获取图像的像素颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文