如何在android中使用自定义大小保存捕获的图像
在我的应用程序中,我可以打开相机拍照。图片以2448x326像素的全尺寸存储在sd卡上。如何在我的应用程序中进行配置,以将图片保存为90x90像素而不是2448x326像素 要打开相机并拍摄图像,我使用以下方法:如何在android中使用自定义大小保存捕获的图像,android,android-listview,android-camera,android-bitmap,android-capture,Android,Android Listview,Android Camera,Android Bitmap,Android Capture,在我的应用程序中,我可以打开相机拍照。图片以2448x326像素的全尺寸存储在sd卡上。如何在我的应用程序中进行配置,以将图片保存为90x90像素而不是2448x326像素 要打开相机并拍摄图像,我使用以下方法: /* * Capturing Camera Image will lauch camera app requrest image capture */ private void captureImage() { Intent intent = new Intent(Medi
/*
* Capturing Camera Image will lauch camera app requrest image capture
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
private Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
}
else {
return null;
}
return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
/*
try {
decodeUri(this, fileUri, 90, 90);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
// successfully captured the image
Toast.makeText(getApplicationContext(),
"Picture successfully captured", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT).show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
}
}
}
public static Bitmap decodeUri(Context c, Uri uri, final int requiredWidth, final int requiredHeight) throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if(width_tmp / 2 < requiredWidth || height_tmp / 2 < requiredHeight)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
/*
*捕获相机图像将启动相机应用程序请求图像捕获
*/
私有void captureImage(){
意向意向=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
fileUri=getOutputMediaFileUri(媒体类型图像);
intent.putExtra(MediaStore.EXTRA_输出,fileUri);
//启动图像捕获计划
startActivityForResult(意图、摄像头捕捉、图像、请求、代码);
}
私有Uri getOutputMediaFileUri(int类型){
返回Uri.fromFile(getOutputMediaFile(类型));
}
私有文件getOutputMediaFile(int类型){
//外部SD卡位置
File mediaStorageDir=新文件(Environment.getExternalStoragePublicDirectory
(环境.目录\图片),图像\目录\名称);
//如果存储目录不存在,请创建该目录
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
Log.d(IMAGE\u DIRECTORY\u NAME,“Oops!创建失败”+IMAGE\u DIRECTORY\u NAME+“DIRECTORY”);
返回null;
}
}
//创建媒体文件名
String timeStamp=new SimpleDateFormat(“yyyyMMdd_HHmmss”,Locale.getDefault()).format(new Date());
文件媒体文件;
如果(类型==媒体类型图像){
mediaFile=新文件(mediaStorageDir.getPath()+File.separator+“IMG_”+timeStamp+“.jpg”);
}
否则{
返回null;
}
返回媒体文件;
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//如果结果是捕获图像
if(requestCode==摄像机捕捉图像请求代码){
if(resultCode==RESULT\u OK){
/*
试一试{
decodeUri(this,fileUri,90,90);
}catch(filenotfounde异常){
e、 printStackTrace();
}
*/
//成功捕获图像
Toast.makeText(getApplicationContext(),
“图片成功捕获”,Toast.LENGTH_SHORT.show();
}else if(resultCode==RESULT\u取消){
//用户取消图像捕获
Toast.makeText(getApplicationContext(),
“用户已取消图像捕获”,Toast.LENGTH_SHORT.show();
}否则{
//未能捕获图像
Toast.makeText(getApplicationContext(),
“抱歉!未能捕获图像”,Toast.LENGTH_SHORT.show();
}
}
}
公共静态位图解码Uri(上下文c、Uri Uri、最终整型requiredWidth、最终整型requiredHeight)引发FileNotFoundException{
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(c.getContentResolver().openInputStream(uri),null,o);
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度| tmp/2<要求宽度|高度| tmp/2<要求高度)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri),null,o2);
}
@凌驾
RestoreInstanceState上的受保护无效(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
//获取文件url
fileUri=savedInstanceState.getParcelable(“文件uri”);
}
我希望s.o.能帮我解决这个问题。我正在尝试将捕获的图像加载到一个小imageview中,如图所示。提前感谢这里,我给出了一种方法,它将获取已拍摄照片SD卡上保存的路径,并将所需大小的图像作为位图返回。现在,您需要做的就是在SD卡上传递图像路径,并获得调整大小的图像
private Bitmap processTakenPicture(String fullPath) {
int targetW = 90; //your required width
int targetH = 90; //your required height
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fullPath, bmOptions);
int scaleFactor = 1;
scaleFactor = calculateInSampleSize(bmOptions, targetW, targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor * 2;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(fullPath, bmOptions);
return bitmap;
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,
int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
阅读原始图像后,您可以使用:
Bitmap.createScaledBitmap(photo, width, height, true);
这是另一个男人有同样问题的时候。他使用下面的方法
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
不,使用MediaStore.ACTION\u IMAGE\u CAPTURE时无法控制图片大小。如果您实现了您的(并且在Internet上有很多工作示例),您可以实现这一点,包括
onPictureTaken()
中接收的字节数组是Jpeg缓冲区。看看这个用于图像处理的Java包:(有一个Android端口)。有非常强大和有效的方法来缩小Jpeg,而无需将其解码为位图并返回。Bitmap scaled=Bitmap.createScaledBitmap(大位图、高度、宽度、真值);西弗兰肯斯汀,谢谢你的帮助。看下面我的答案没人能帮我吗?我想要的只是一个已保存的捕获图像的缩略图,以便在自定义listview中使用。在捕获初始图像后,您不能创建缩放位图吗?很遗憾,不能。因为我不知道在何处以及如何将其保存为位图。sd卡上保存的图像为.jpg格式。在将其保存到sd卡之前,可以将其调整为缩略图格式吗?谢谢你,哈米德,但我想在开始时将其保存为自定义大小。这就是我想要的。有没有办法做到这一点?我正在使用simplecursor适配器加载自定义listview中的信息。这意味着,我必须提供一个资源id(例如:R.id.image),感谢u mina的帮助。我想将其保存为自定义尺寸。见上面我的答案。