我遇到的问题是我使用相机意图拍摄的图像会被旋转,我在Why does an image captured using camera intent gets rotated on some devices on Android?询问了
因此根据建议的答案,我现在正在尝试旋转使用相机拍摄的图像(我使用了相机意图)。我的代码是,
public class SimpleCameraActivity extends Activity {
private static final int TAKE_PICTURE = 1888;
private Uri imageUri;
ImageView imageView;
Bitmap bitmap;
Button btnOK, btnDiscard;
RelativeLayout rLButtons;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_camera);
imageView = (ImageView) findViewById(R.id.image_view_sc);
captureImage();
}
public void captureImage() {
Intent intentCamera = new Intent("android.media.action.IMAGE_CAPTURE");
File filePhoto = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
imageUri = Uri.fromFile(filePhoto);
MyApplicationGlobal.imageUri = imageUri.getPath();
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intentCamera, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentFromCamera) {
super.onActivityResult(requestCode, resultCode, intentFromCamera);
if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
if (intentFromCamera != null) {
Bundle extras = intentFromCamera.getExtras();
if (extras.containsKey("data")) {
bitmap = (Bitmap) extras.get("data");
} else {
bitmap = getBitmapFromUri();
}
} else {
bitmap = getBitmapFromUri();
}
checkForRotation();
//rotateImage();
//rotateImage1();
rotateImage3();
//imageView.setImageBitmap(bitmap);
// imageView.setImageURI(imageUri);
} else {
finish();
}
}
public void rotateImage() {
Bitmap targetBitmap = Bitmap.createBitmap(100, 80, bitmap.getConfig());
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(180, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
matrix.setRotate(90);
canvas.drawBitmap(bitmap, matrix, new Paint());
imageView.setImageBitmap(bitmap);
}
public void rotateImage1() {
Matrix matrix = new Matrix();
matrix.setRotate(90, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
matrix.mapRect(rectF);
Bitmap targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), bitmap.getConfig());
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(bitmap, matrix, new Paint());
imageView.setImageBitmap(bitmap);
}
public void rotateImage2() {
Matrix mtx = new Matrix();
mtx.reset();
mtx.preTranslate(20, 30);
mtx.setRotate((float) 90, 25, 35);
mtx.postTranslate(30, 40);
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, 100, 100, mtx, true);
this.bitmap = rotatedBMP;
imageView.setImageBitmap(bitmap);
}
public void rotateImage3() {
Bitmap bmpOriginal = bitmap;
Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()), (bmpOriginal.getHeight()), Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setAntiAlias(true);
Matrix matrix = new Matrix();
matrix.setRotate((float) 90, (float) (bmpOriginal.getWidth() / 2), (float) (bmpOriginal.getHeight() / 2));
RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
matrix.mapRect(rectF);
targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), Bitmap.Config.ALPHA_8);
Canvas tempCanvas = new Canvas(targetBitmap);
tempCanvas.drawBitmap(bmpOriginal, matrix, p);
imageView.setImageBitmap(bmpOriginal);
}
public Bitmap getBitmapFromUri() {
getContentResolver().notifyChange(imageUri, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
我尝试了不同的代码rotateImage,1,2,3等。但是没有代码正常工作。 rotateImage2()正在工作,但它严重影响质量,所以不能使用它。
我从Android: How to rotate a bitmap on a center point获得了这些代码,用户说它的工作正常,但不是我的工作。 那我错了。
答案 0 :(得分:8)
坚持使用 rotateImage2()并使用较少的转换。这段代码适合我:
private Bitmap rotateImage(String pathToImage) {
// 1. figure out the amount of degrees
int rotation = getImageRotation();
// 2. rotate matrix by postconcatination
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
// 3. create Bitmap from rotated matrix
Bitmap sourceBitmap = BitmapFactory.decodeFile(pathToImage);
return Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
}
获取图像旋转check out your other post。有关Matrix操作及其差异的更多信息,例如setRotate和postRotate check out this post。