gpt4 book ai didi

android - 用相机拍照并编辑

转载 作者:行者123 更新时间:2023-11-30 03:51:50 27 4
gpt4 key购买 nike

我需要拍照并编辑它。我使用原始相机并返回大尺寸图像,所以当我尝试创建新的模板图像进行编辑时,应用程序将因为 outOfMemoryError 而关闭。在将图像加载到我的应用程序之前,有什么方法可以帮助我调整图像大小吗?

我的代码在相机中拍摄图像:

void loadImageFromCamera(){
Intent takePicture = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = null;
try{
// place where to store camera taken picture
photo = this.createTemporaryFile("picture", ".jpg");
photo.delete();
} catch(Exception e){

}
mImageUri = Uri.fromFile(photo);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(takePicture, 0);
}
private File createTemporaryFile(String part, String ext) throws Exception{
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
if(!tempDir.exists()){
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
public void grabImage(){
this.getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
try {
originImage = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
}catch (Exception e){

}

}

最佳答案

是的,您可以调整图像大小使用以下方法调整图像大小

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}

关于android - 用相机拍照并编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com