使用拍摄照片作为来源后,ImageView尺寸非常小

时间:2016-04-22 10:26:54

标签: android bitmap imageview android-camera

我正在尝试创建一个按下按钮时正在访问相机的应用,并且在拍摄照片后,将其显示在屏幕上,然后能够找到所选像素的颜色

不知何故,显示的照片非常小,即使我选择的图像不是来自图像,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" />

1 个答案:

答案 0 :(得分:0)

使用此代码可能是其正常工作

WHERE "TIMESTAMP" >= DATE '2016-03-01'
AND   "TIMESTAMP" <  DATE '2016-05-01'