简体   繁体   English

从相机到Imageview的图像分辨率

[英]Resolution of image from camera to Imageview

I want to take a picture and add image to the activity. 我想拍照并将图像添加到活动中。 I was having a weird pixelation and at first i thought that it was something with the blob I used, but it seems like it has to do with the image after capture from camera. 我当时有一个奇怪的像素化现象,起初我以为这与我使用的斑点有关,但似乎与从相机捕获后的图像有关。

I activate the camera to take a picture 我启动相机拍照

Camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });

Then I want to set image at the imageview. 然后我想在imageview设置图像。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            int width = photo.getWidth();
            int height = photo.getHeight();
            Toast.makeText(getApplicationContext(),"Width:"+width+" / Height:"+height,Toast.LENGTH_SHORT).show();
            Screen.setImageBitmap(photo);
        }

    }

After I take the image and there is a preview to verify that I want the image the picture gets pixelated and the resolution of the bitmap I get is 150x205 and I don't know what have I done wrong. 拍摄完图像并进行预览后,可以验证图像是否被像素化,并且得到的位图分辨率为150x205,我不知道自己做错了什么。

I uploaded a small video to see the actual resolution http://youtu.be/s5Cu3QYSDto 我上传了一个小视频,以查看实际分辨率http://youtu.be/s5Cu3QYSDto

In android the default data which you get from camera is low resolution thumbnail image. 在android中,您从相机获取的默认数据是低分辨率缩略图。

So before you call CameraIntent create a file and uri based on that filepath as shown here. 因此,在调用CameraIntent之前,请根据该文件路径创建一个文件和uri,如下所示。

filename = Environment.getExternalStorageDirectory().getPath() + "/folder/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));

// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                imageUri);
startActivityForResult (cameraIntent, CAMERA_REQUEST);

Now, you have the filepath you can use it in onAcityvityResult method as following,you can also get the bitmap from the uri. 现在,您具有可以在onAcityvityResult方法中使用的文件路径,如下所示,还可以从uri获取位图。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    ImageView img = (ImageView) findViewById(R.id.image);
    img.setImageURI(imageUri);
    }
}

I will show you the exact code I used by following the developer page . 我将通过遵循开发人员页面向您显示我使用的确切代码。

I call the dispatchTakePictureIntent class. 我调用dispatchTakePictureIntent类。

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.d("Error creating image file","CAM");
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                uriphoto = Uri.fromFile(photoFile);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

and the createImageFile class createImageFile

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

and for the onActivityResult class. 以及onActivityResult类。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
            Screen.setImageURI(uriphoto);
        }
        else  if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            Screen.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }

It will create an image file in the gallery folder but won't be displayed on gallery app itself. 它将在gallery文件夹中创建一个图像文件,但不会显示在gallery app本身上。

I hope it helps somebody but still i'm sure there are better methods to do what I wanted to do but for sure it works. 我希望它可以对某人有所帮助,但是我仍然确定有更好的方法来完成我想做的事情,但是可以肯定它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM