android - 使用 setPictureSize 和相机参数

标签 android object camera

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

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

最佳答案

当您通过自定义 View 实际使用相机时会用到它。您在这里所做的是向 Android 发送隐式 Intent 以打开另一个使用相机拍照的 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);

然后将其保存在旧的上。

关于android - 使用 setPictureSize 和相机参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7729461/

相关文章:

Android:如何使任务管理器和菜单中的应用程序不可见

c# - 不一致的可访问性 : Parameter type is less accessible than method

java - CameraBridgeViewBase 类的 setMaxFrameSize 方法在 OpenCV Java 中到底做了什么?

iphone - iOS 所需的设备功能自动对焦相机

ios - 自定义 iPhone 相机控件(不使用 UIImagePickerController)

android - 纬度和经度在 Android 中显示为 0

android - 如何在android中使用mpchart库设置图表外的矩形框?

javascript - 在 react-native 中更改导航标题栏标题的字体

php - 如何在 PHP 中将对象转换(转换)为没有类名前缀的数组?

ios - 如何检查 NSArray 中的空对象