Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置安卓相机拍摄的照片的大小和质量?_Android_Image_Android Camera_Crop_Image Quality - Fatal编程技术网

Android 如何设置安卓相机拍摄的照片的大小和质量?

Android 如何设置安卓相机拍摄的照片的大小和质量?,android,image,android-camera,crop,image-quality,Android,Image,Android Camera,Crop,Image Quality,我编写了这段代码,用相机拍摄一张照片,然后将其转换为jpeg文件,并将其作为Parse文件上传到Parse.com 问题是图片非常小(153 x 204像素),质量非常低 我需要的图片是至少5兆帕的质量,并裁剪到300x300像素 这是我现在使用的代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

我编写了这段代码,用相机拍摄一张照片,然后将其转换为jpeg文件,并将其作为Parse文件上传到Parse.com

问题是图片非常小(153 x 204像素),质量非常低

我需要的图片是至少5兆帕的质量,并裁剪到300x300像素

这是我现在使用的代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_picture);

        img = (ImageView) findViewById(R.id.imgV);

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        byte[] image_byte_array;
              ParseObject post_object = new ParseObject("Gallery");
            Bundle extras = data.getExtras();
            Bitmap image = (Bitmap) extras.get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            image_byte_array = stream.toByteArray();
            ParseFile picture_file = new ParseFile("Picture.jpg", image_byte_array);
            picture_file.saveInBackground();
            post_object.put("photo", picture_file);
            post_object.saveInBackground();
        }
提前感谢。

查看下面的链接

您必须添加一个
Uri
,它是存储照片的文件的Uri

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
完整样品

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_picture);

    img = (ImageView) findViewById(R.id.imgV);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}
检查

默认情况下,相机会返回缩略图,因此需要 获取完整图像

德里克的解决方案是正确的,但你必须改进它。

真正的乐趣在于获取这里发生的完整图像-

thumbnail = MediaStore.Images.Media.getBitmap(
                            getContentResolver(), imageUri);
                    imgView.setImageBitmap(thumbnail);
                    imageurl = getRealPathFromURI(imageUri);    

完整路径将由
getRealPathFromURI()

提供。我不想将图片保存到手机存储器中,我只需要将图片与我显示的代码一起直接上传到Parse.com服务器,因此如果您知道我做错了什么,我将不胜感激。通过使用相机意图,您无法避免将图片保存到存储器中。如果你真的想跳过保存到存储,你必须实现你自己的相机。在
onActivityResult
中返回的
数据
只是一个缩略图,这就是为什么它这么小的原因。好的,谢谢你,@Darpan linked的答案对我帮助很大,仍然谢谢;)谢谢,我会选择正确的答案,因为你链接的答案帮助了我,谢谢;)小贴士-尝试谷歌多一点。在android中,除非它是一个非常新的API;所有的问题都已经回答了。对我们来说,这是一个巨大的支持基础:)