這個代碼的問題是,它只獲取所有圖片的顏色值,而不是用戶點擊/觸摸的地方! 一定有一個簡單的問題,我相信! 下面是代碼,讓我們看看你可以得到什麼! )從圖庫中導入的圖像中獲取Android中的像素顏色?
import java.io.FileNotFoundException;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MPActivity extends Activity {
private TextToSpeech mTts;
private ImageView targetImage;
private TextView textView;
private TextView textViewCol;
private TextView textViewVal;
private boolean hasTTSBeenInitialized = false;
private int imageWidth;
private int imageHeight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textView = (TextView) findViewById(R.id.textView);
textViewCol = (TextView) findViewById(R.id.textViewColor);
targetImage = (ImageView) findViewById(R.id.targetimage);
textViewVal = (TextView) findViewById(R.id.textViewValue);
targetImage = (ImageView)findViewById(R.id.targetimage);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});
mTts = new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
hasTTSBeenInitialized = true;
}
});
mTts.setLanguage(Locale.US);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
//w2,h2
imageWidth = bitmap.getWidth();
imageHeight = bitmap.getHeight();
targetImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x = ((int)event.getX()/v.getWidth()) * imageWidth;
int y = ((int)event.getY()/v.getHeight()) * imageHeight;
textView.setText("Touch coordinates : "+String.valueOf(event.getX())+"x"+String.valueOf(event.getY()));
ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
// Calculation -> x, y - image space
int pixel = bitmap.getPixel(x,y);
textViewVal.setText("[" + Color.red(pixel) + "," + Color.green(pixel) + "," + Color.blue(pixel) + "]");
if ((Color.red(pixel) > 200) && (Color.blue(pixel) < 10) && (Color.green(pixel) < 10)) {
if(hasTTSBeenInitialized){
mTts.speak("Color is RED", TextToSpeech.QUEUE_FLUSH, null);
}
textViewCol.setText("Color is Red.");
}
return true; }
});
}
}
可能是我太累了,但我找不到代碼中的任何地方,你給x和y賦值。 – HenrikS
在這裏,我更新了我的代碼,我也把RED作爲一個例子來讀取該程序...謝謝你... – user1056066