ich ein Programm erstellen will, das dies tut:Benachrichtigung, wenn eine bestimmte Farbe von einem Pixel ausgewählt ist
- take Bild;
- Bereich auswählen, um Farbe zu erhalten;
- die App die Benutzer informieren, das ist die aktuelle Weißton und wählen Sie die Schatten auf der Option basieren wir für die App erhalten
Was ich versuche zu tun, um eine Nachricht zu erhalten, um zu sehen, ob die ausgewählte Farbe (von der berührten Position) ist näher an einer von mir implementierten Schattierung von Weiß (sagen wir, wenn die berührte Farbe zwischen #FCFFFFFF und # FCFEF9D5 ist, wird das Programm "A2" anzeigen).
Dies ist mein Code so weit:
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;
}
}