从相机到Imageview的图像分辨率

时间:2015-02-27 17:18:56

标签: android android-imageview pixels

我想拍照并为活动添加图片。我有一个奇怪的像素化,起初我认为它与我使用的blob有关,但它似乎与从相机捕获后的图像有关。

我激活相机拍照

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);
            }
        });

然后我想在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);
        }

    }

在我拍摄图像后,有一个预览验证我想要图像,图片会变得像素化,我得到的位图分辨率是150x205,我不知道我做错了什么。

我上传了一个小视频,以查看实际的分辨率http://youtu.be/s5Cu3QYSDto

2 个答案:

答案 0 :(得分:3)

在android中,你从相机获得的默认数据是低分辨率缩略图。

因此,在调用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);

现在,您可以在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);
    }
}

答案 1 :(得分:0)

我将按照developer page显示我使用的确切代码。

我调用 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);
            }
        }
    }

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;
    }

以及 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));

        }

    }

它将在图库文件夹中创建一个图像文件,但不会在图库应用程序本身上显示。

我希望它可以帮助某些人,但我仍然确信有更好的方法可以做我想做的事情,但肯定会有效。