Android 使用setPictureSize和摄影机参数

Android 使用setPictureSize和摄影机参数,android,object,camera,Android,Object,Camera,我研究了如何设置图片的大小,偶然发现了CameraParameters和与之相关的setPictureSize方法。问题是,我不知道如何使用这些。我不知道需要导入什么,创建什么对象,如何使用setPictureSize方法,甚至不知道将代码放在哪里。我有两段代码,我觉得这可能是使用setPictureSize的好地方。这些区块是: takePicture = (Button) findViewById(R.id.takePicture); takePicture.setOnCl

我研究了如何设置图片的大小,偶然发现了CameraParameters和与之相关的setPictureSize方法。问题是,我不知道如何使用这些。我不知道需要导入什么,创建什么对象,如何使用setPictureSize方法,甚至不知道将代码放在哪里。我有两段代码,我觉得这可能是使用setPictureSize的好地方。这些区块是:

    takePicture = (Button) findViewById(R.id.takePicture);

    takePicture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (name.getText().length() == 0 || experimentInput.getText().length() == 0) {
                showDialog(DIALOG_REJECT);
                return;
            }

            ContentValues values = new ContentValues();
            imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

            time = getTime() ;

            startActivityForResult(intent, CAMERA_PIC_REQUESTED);
        }

    });
以及:

所以,我的问题是,如何使用setPictureSize以及应该将其放在哪里?没有网站,也没有android开发指南,也没有任何其他相关的StackOverflow问题对我有帮助

编辑代码:

public Bitmap getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            int desiredImageWidth = 100;  // pixels
            int desiredImageHeight = 100; // pixels
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL
            Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(cursor.getString(column_index), o), 
                                                       desiredImageWidth, 
                                                       desiredImageHeight, 
                                                       false);

            return newImage; //cursor.getString(column_index);
        }
        else return null;
    }

当您通过自定义
视图实际使用相机时,可使用此选项。你现在正在做的是向Android发送一个隐含的意图,即打开另一个使用相机拍照的
活动

在这种情况下,您可以使用
MediaStore.EXTRA\u size\u limit
来限制图像的大小。如果要指定图像的尺寸,则必须从保存图像的URI加载图像,创建新的缩放图像,然后在旧图像上重新保存

获得图像的URI(来自投影中的
数据
属性)后,可以按如下方式调整图像大小:

int desiredImageWidth = 100;  // pixels
int desiredImageHeight = 100; // pixels
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL
Bitmap newImage = Bitmap.createScaleBitmap(BitmapFactory.decodeFile(imagePath, o), 
                                           desiredImageWidth, 
                                           desiredImageHeight, 
                                           false);

然后把它保存在旧的上面。

听起来很合理。我到底把这个放在哪里了?在
convertmageuritofile
方法或
getPath
方法中的某个地方?我个人不会将位图编码放在这些方法中的任何一个方法中,因为每个服务器都有自己的用途
imagePath
必须是直接指向图像文件的字符串对象(而不是android内容uri),并且它看起来像是
getPath()
将为您准确地检索该对象。您还应该能够从
文件
对象中获取该信息。您的
convertImageUriToFile()
方法使用
getAbsolutePath()
getPath()
返回该信息。我有点理解您的意思。要实现您提到的功能,我必须使用字符串对象,而不是android内容uri。我的问题是,作为一名Android程序员新手,我需要做什么改变才能做到这一点?是否需要将
String[]projection
放在某处/将其返回到某处?如果成功,您的
getPath()
方法将返回所需的字符串。如果需要,可以将其更改为
getBitmap
type方法,并使用
cursor.getString(column\u index)作为图像路径。我在问题的底部添加了一个编辑。这就是你的意思吗?
int desiredImageWidth = 100;  // pixels
int desiredImageHeight = 100; // pixels
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 2; // will cut the size of the image in half; OPTIONAL
Bitmap newImage = Bitmap.createScaleBitmap(BitmapFactory.decodeFile(imagePath, o), 
                                           desiredImageWidth, 
                                           desiredImageHeight, 
                                           false);