旋转文件中的图像 Android
Rotating image in File for Android
我在裁剪图像之前尝试旋转图像时遇到问题。我试过在我想旋转的图像视图上调用 setRotation
,它成功地旋转了图像视图。我意识到这不是解决方案,因为当我调用 CropImageIntentBuilder 时,它会在没有任何旋转的情况下传入原始图像视图。
在下面的代码中,我尝试了一种不同的方法,将图像保存到文件并将该文件传递给 CropImageIntentBuilder,但我仍然遇到同样的问题。请让我知道您是否可以就此提供任何建议,或者我是否以错误的方式处理此问题。另外,如果我需要 post 更多来自应用程序的代码,请告诉我。
public static Bitmap rotateBitmap (Bitmap source, float angle){
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),matrix,true);
}
private void executeCropImageIntent() {
//This is the cropIntent that is called using nexuss 10
try{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
//mSelectedImage.setImageBitmap(rotateBitmap(bitmap, 90));
rotateBitmap(bitmap, 90);
try{
FileOutputStream fOut = new FileOutputStream(mSingleton.mCropFileTemp);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();}
catch (Exception e) {
e.printStackTrace();
Log.i(null, "Save file error!");
// Toast.makeText(this, getResources().getString(R.string.messageMissingGPS), Toast.LENGTH_LONG).show();
}
}
catch (IOException e) {
Log.d("JudgeMainFragment", "cannot take picture", e);
}
CropImageIntentBuilder intentBuilder = new CropImageIntentBuilder(0, 0, 0, 0, Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
// intentBuilder.setSourceImage(Uri.parse(mData.getImage()));
intentBuilder.setSourceImage(Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
startActivityForResult(intentBuilder.getIntent(this), IJudgeSingleton.REQUEST_CODE_CROP_IMAGE);
}
您正在呼叫 rotateBitmap(bitmap, 90);
。但不将返回的 BitMap
分配给任何变量。然后您正在使用未旋转的旧位图。所以改变这一行
rotateBitmap(bitmap, 90);
进入这个
bitmap = rotateBitmap(bitmap, 90);
我在裁剪图像之前尝试旋转图像时遇到问题。我试过在我想旋转的图像视图上调用 setRotation
,它成功地旋转了图像视图。我意识到这不是解决方案,因为当我调用 CropImageIntentBuilder 时,它会在没有任何旋转的情况下传入原始图像视图。
在下面的代码中,我尝试了一种不同的方法,将图像保存到文件并将该文件传递给 CropImageIntentBuilder,但我仍然遇到同样的问题。请让我知道您是否可以就此提供任何建议,或者我是否以错误的方式处理此问题。另外,如果我需要 post 更多来自应用程序的代码,请告诉我。
public static Bitmap rotateBitmap (Bitmap source, float angle){
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),matrix,true);
}
private void executeCropImageIntent() {
//This is the cropIntent that is called using nexuss 10
try{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
//mSelectedImage.setImageBitmap(rotateBitmap(bitmap, 90));
rotateBitmap(bitmap, 90);
try{
FileOutputStream fOut = new FileOutputStream(mSingleton.mCropFileTemp);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();}
catch (Exception e) {
e.printStackTrace();
Log.i(null, "Save file error!");
// Toast.makeText(this, getResources().getString(R.string.messageMissingGPS), Toast.LENGTH_LONG).show();
}
}
catch (IOException e) {
Log.d("JudgeMainFragment", "cannot take picture", e);
}
CropImageIntentBuilder intentBuilder = new CropImageIntentBuilder(0, 0, 0, 0, Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
// intentBuilder.setSourceImage(Uri.parse(mData.getImage()));
intentBuilder.setSourceImage(Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
startActivityForResult(intentBuilder.getIntent(this), IJudgeSingleton.REQUEST_CODE_CROP_IMAGE);
}
您正在呼叫 rotateBitmap(bitmap, 90);
。但不将返回的 BitMap
分配给任何变量。然后您正在使用未旋转的旧位图。所以改变这一行
rotateBitmap(bitmap, 90);
进入这个
bitmap = rotateBitmap(bitmap, 90);