I want to create a program that does this:
What I'm trying to do is to get a message to see if the selected color (from the touched position) is closer to a shade of white that I'm implementing (let's say, if the touched color is between #FCFFFFFF and #FCFEF9D5 the program will display "A2").
This is my code so far:
public class MainActivity extends Activity {
TextView touchedXY, invertedXY, imgSize, colorRGB;
ImageView imgSource1;
Button b, bOpenFromGallery;
static final int CAMREQUEST = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
touchedXY = (TextView) findViewById(R.id.xy);
invertedXY = (TextView) findViewById(R.id.invertedxy);
imgSize = (TextView) findViewById(R.id.size);
colorRGB = (TextView) findViewById(R.id.colorrgb);
b = (Button)findViewById(R.id.Button01);
bOpenFromGallery = (Button)findViewById(R.id.button_openFromGallery);
imgSource1 = (ImageView)findViewById(R.id.source1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMREQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMREQUEST) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Bitmap newBitmap = BITMAP_RESIZER(bitmap, 1280, 768);
imgSource1.setImageBitmap(newBitmap);
imgSource1.setOnTouchListener(imgSourceOnTouchListener);
}
}
}
OnTouchListener imgSourceOnTouchListener
= new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[]{eventX, eventY};
Matrix invertMatrix = new Matrix();
((ImageView) view).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int) eventXY[0]);
int y = Integer.valueOf((int) eventXY[1]);
touchedXY.setText(
"touched position: "
+ String.valueOf(eventX) + " / "
+ String.valueOf(eventY));
invertedXY.setText(
"touched position: "
+ String.valueOf(x) + " / "
+ String.valueOf(y));
Drawable imgDrawable = ((ImageView) view).getDrawable();
Bitmap bitmap = ((BitmapDrawable) imgDrawable).getBitmap();
imgSize.setText(
"drawable size: "
+ String.valueOf(bitmap.getWidth()) + " / "
+ String.valueOf(bitmap.getHeight()));
//Limit x, y range within bitmap
if (x < 0) {
x = 0;
} else if (x > (bitmap.getWidth() - 1)) {
x = bitmap.getWidth() - 1;
}
if (y < 0) {
y = 0;
} else if (y > (bitmap.getHeight() - 1)) {
y = bitmap.getHeight() - 1;
}
int touchedRGB = bitmap.getPixel(x, y);
colorRGB.setText("touched color: " + "#" + Integer.toHexString(touchedRGB));
colorRGB.setTextColor(touchedRGB);
return true;
}
};
public Bitmap BITMAP_RESIZER(Bitmap bitmap,int newWidth,int newHeight) {
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
float ratioX = newWidth / (float) bitmap.getWidth();
float ratioY = newHeight / (float) bitmap.getHeight();
float middleX = newWidth / 2.0f;
float middleY = newHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
}
答案 0 :(得分:1)
As here said, you can get your image top-left corner, then calculate the touched position. After that, u can get the color of that area by doing this:
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
you can filter color like this:
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);