Ich versuche, eine App zu erstellen, die auf die Kamera zugreift, wenn eine Taste gedrückt wird, und nachdem das Foto aufgenommen wurde, um es auf dem Bildschirm anzuzeigen, und dann in der Lage sein, die Farbe des ausgewählten Pixels zu finden.ImageView Größe ist sehr klein nach der Verwendung eines aufgenommenen Foto als Quelle
Irgendwie ist das angezeigte Foto sehr klein, und die x, y zeigen Farben an, selbst wenn ich ein Pixel wähle, das nicht vom Bild stammt. Ich habe versucht, die Höhe und Breite des Layouts auf einen bestimmten Wert zu ändern, und das Foto ist pixelig, auch wenn die Auflösung des Fotos ziemlich groß ist.
Ich möchte in der Lage sein, die Pixelfarbe nur vom Bild, nicht außerhalb davon, korrekt anzuzeigen und das Bild auf dem Bildschirm gut zu skalieren.
Hier ist mein Code, hoffe ihr könnt mir helfen, ich bin ein Neuling. Vielen Dank!
MainActivity.Java
public class MainActivity extends Activity {
TextView touchedXY, invertedXY, imgSize, colorRGB;
ImageView imgSource1;
Button b;
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);
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 (requestCode == CAMREQUEST) {
Bitmap image = (Bitmap) data.getExtras().get("data");
imgSource1 = (ImageView) findViewById(R.id.source1);
imgSource1.setImageBitmap(image);
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;
}
};
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ffffff">
<TextView
android:id="@+id/xy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="touched position: "
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/invertedxy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inverted touched position: "
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="drawable size: "
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/colorrgb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="touched color: "
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp" />
<ImageView
android:id="@+id/source1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/Button01"
android:layout_gravity="center_horizontal" />
MediaPath wird nicht erklärt, auch " Absicht "wird nicht erklärt, also ich ge t Kompilierungsfehler! Auch in der OnActivityResult-Methode haben wir "Bitmap Bitmap ..." und dann imgSource1.setImageBitmap (Bild). "Bild" sollte nicht "Bitmap" sein? –