2011-11-20 146 views
0

這個代碼的問題是,它只獲取所有圖片的顏色值,而不是用戶點擊/觸摸的地方! 一定有一個簡單的問題,我相信! 下面是代碼,讓我們看看你可以得到什麼! )從圖庫中導入的圖像中獲取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; }  
}); 


} 

} 
+0

可能是我太累了,但我找不到代碼中的任何地方,你給x和y賦值。 – HenrikS

+0

在這裏,我更新了我的代碼,我也把RED作爲一個例子來讀取該程序...謝謝你... – user1056066

回答

2

看起來與整數除法的問題。這個短語可能產生零:

(int)event.getX()/v.getWidth() 

既然你已經投getX()價值的intgetWidth()返回一個int,整數除法開始,這意味着結果被截斷到最近較小的整數。相反,在分區之前先進行乘法運算,並保持演員陣容持續。

int x = (int) (event.getX() * imageWidth/v.getWidth()); 
int y = (int) (event.getY() * imageHeight/v.getHeight()); 
+0

我很欣賞它,它工作得很好......但對不起,我的聲望太低了不能投票給你的答案! – user1056066

+0

很高興我的回答很有幫助。您應該可以通過點擊旁邊的複選標記來接受它。 – kabuko

相關問題