使用setPictureSize和Camera Parameters

时间:2011-10-11 16:37:30

标签: android object camera

我已经研究了如何设置图片的大小,我偶然发现了CameraParameters和与之关联的setPictureSize方法。问题是,我无法弄清楚如何使用其中任何一个。我不知道需要导入什么,要创建什么对象,如何使用setPictureSize方法,或者甚至放置代码。我有2块代码,我觉得这可能是使用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);
        }

    });

public static File convertImageUriToFile (Uri imageUri, Activity activity)  {
    if (activity == null) Log.d("test", "NULL!");
    Cursor cursor = null;
    try {

        String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
        cursor = activity.managedQuery( imageUri,
                proj, // Which columns to return
                null,       // WHERE clause; which rows to return (all rows)
                null,       // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            @SuppressWarnings("unused")
                String orientation =  cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

public String 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();
        return cursor.getString(column_index);
    }
    else return null;
}

所以,我的问题是,我如何使用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;
    }

1 个答案:

答案 0 :(得分:2)

当您通过自定义View实际使用相机时,用于此功能。你在这里做的是向Android发送一个隐含的意图,打开另一个使用相机拍照的Activity

在这种情况下,您可以使用MediaStore.EXTRA_SIZE_LIMIT限制图片的大小。如果要指定图像的尺寸,而不是必须从保存的URI中加载它,请创建一个新的缩放图像,然后将其重新保存在旧图像上。

获得图像的URI(来自投影中的DATA属性)后,您可以像这样调整图像大小:

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

然后把它保存在旧的。