0
ボタンを押したときにカメラにアクセスするアプリを作成しようとしていて、写真を撮ってから画面に表示すると選択されたピクセルの色を見つけることができます。撮影した写真をソースとして使用した後、ImageViewのサイズが非常に小さい
どういうわけか、表示されている写真が非常に小さく、画像から来ていないピクセルを選択していてもx、yが色を表示しています。 レイアウトの高さと幅を特定の値に変更しようとしましたが、写真の解像度がかなり高い場合でも写真はピクセル化されています。
画像のピクセル色を正しく表示することができます。画像の外には表示されず、画像は画面上に正しく表示されます。
ここに私のコードは、皆さんが私を助けることができることを願って、私は初心者です。大いに感謝する!
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" />
メディアパスは、「また、宣言されていないかもしれインテント "は宣言されていないので、コンパイルエラー!また、onActivityResultメソッドでは、 "Bitmap bitmap ..."を取得し、次にimgSource1.setImageBitmap(image)を取得します。 「イメージ」は「ビットマップ」であってはいけませんか? –