Java 我想在我单击的位置提取图像像素的rgb值,但每当我单击它时。即使所有语法都正确,它也会崩溃
选择颜色 打开相机的按钮 从照相机中取出图像Java 我想在我单击的位置提取图像像素的rgb值,但每当我单击它时。即使所有语法都正确,它也会崩溃,java,android,ontouchlistener,Java,Android,Ontouchlistener,选择颜色 打开相机的按钮 从照相机中取出图像 崩溃时会抛出什么错误/异常?一定有堆栈跟踪。您是否调试了代码?可能与“我已添加断点以调试代码”重复,以及逐步调试代码揭示了什么?您将在运行窗口中找到堆栈跟踪,该窗口通常位于底部。请查找并将堆栈跟踪添加到问题 package com.example.camdetect; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import and
崩溃时会抛出什么错误/异常?一定有堆栈跟踪。您是否调试了代码?可能与“我已添加断点以调试代码”重复,以及逐步调试代码揭示了什么?您将在运行窗口中找到堆栈跟踪,该窗口通常位于底部。请查找并将堆栈跟踪添加到问题
package com.example.camdetect;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Bitmap;
import com.skydoves.colorpickerview.ColorEnvelope;
import com.skydoves.colorpickerview.ColorPickerView;
import com.skydoves.colorpickerview.listeners.ColorListener;
public class MainActivity extends AppCompatActivity {
public int[] s = new int [4];
private static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView imageview;
private TextView i_b,i_g,i_r,p_r,p_g,p_b;//**these are declared globally to avoid declaring it final**
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i_b = this.findViewById(R.id.i_b);
i_g = this.findViewById(R.id.i_g);
i_r = this.findViewById(R.id.i_r);
p_r = this.findViewById(R.id.p_r);
p_g = this.findViewById(R.id.p_g);
p_b = this.findViewById(R.id.p_b);
ColorPickerView colorPickerView = findViewById(R.id.colorPickerView);
imageview = this.findViewById(R.id.imageview);
colorPickerView.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, boolean fromUser) {
ColorEnvelope colorenvelope = new ColorEnvelope(color);
int c[] = colorenvelope.getArgb();
p_r.setText(String.valueOf(c[1]));
p_g.setText(String.valueOf(c[2]));
p_b.setText(String.valueOf(c[3]));
for (int i = 1; i < 4; i++) {
s[i] = c[i];
}
}
});
imageview.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int[] viewCoords = new int[2];
imageview.getLocationOnScreen(viewCoords);
int x = Math.round(event.getX());
int y = Math.round(event.getY());
int imageX = x- viewCoords[0];
int imageY = y - viewCoords[1];
ImageView image = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(imageX,imageY);
int r,g,b;
r = Color.red(pixel);
g = Color.green(pixel);
b = Color.blue(pixel);
i_r.setText(String.valueOf(r));
i_g.setText(String.valueOf(g));
i_b.setText(String.valueOf(b));
if(s[1] == r && s[2] == g && s[3] == b){
Toast.makeText(getApplicationContext(),"Match Found",Toast.LENGTH_SHORT).show();}
else{
Toast.makeText(getApplicationContext(),"Match Not Found",Toast.LENGTH_SHORT).show();}
return true;
}
});
Button opencam = this.findViewById(R.id.opencam);
opencam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap)extras.get("data");
imageview.setImageBitmap(imageBitmap);
}
}
}