- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我研究了如何设置图片的大小,然后偶然发现了 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/
我研究了如何设置图片的大小,然后偶然发现了 CameraParameters 和与之关联的 setPictureSize 方法。问题是,我不知道如何使用这些。我不知道需要导入什么、要创建什么对象、如何
我试图在我的相机对象中设置最佳可能的输出图片尺寸。这样,我就可以得到一个完美的缩小样本图像并显示它。 在调试过程中,我观察到我将输出图片大小设置为与我的屏幕尺寸完全一致。但是当我 DecodeBoun
我是一名优秀的程序员,十分优秀!