如何在保存之前在 android 相机预览应用程序中旋转图像?
How to rotate an image in android camera preview app before save it?
我有一个 Android 相机应用程序,它以纵向模式拍摄照片,相机预览很好......但是当我拍摄照片时它以横向模式保存......它旋转 90 度:我的代码 onPictureTaken():
PictureCallback jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator
+ photoFile;
Log.e("path", filename.toString());
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(arg0);
fos.close();
Bitmap bm = BitmapFactory.decodeFile(filename);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
// sba1 = Base64.encodeBytes(ba);
int flag = 0; // you can pass the default 0 = Base64.DEFAULT
String ba1 = Base64.encodeToString(ba, flag);
// Log.e("base64", "-----" + ba1);
Toast.makeText(AndroidCamera.this,
"New Image saved 22222:" + photoFile, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(AndroidCamera.this,
UploadActivity.class);
fileUri = Uri.fromFile(pictureFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,
CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
launchUploadActivity(true);
} catch (Exception error) {
// Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename +
// "not saved: "
// + error.getMessage());
Toast.makeText(AndroidCamera.this, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
}
};
您可以像这样将位图旋转 90 度:
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
在你的具体例子中,需要存回原来的路径:
Bitmap bm = BitmapFactory.decodeFile(filename);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
FileOutputStream fos = new FileOutputStream(pictureFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
我有一个 Android 相机应用程序,它以纵向模式拍摄照片,相机预览很好......但是当我拍摄照片时它以横向模式保存......它旋转 90 度:我的代码 onPictureTaken():
PictureCallback jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator
+ photoFile;
Log.e("path", filename.toString());
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(arg0);
fos.close();
Bitmap bm = BitmapFactory.decodeFile(filename);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
// sba1 = Base64.encodeBytes(ba);
int flag = 0; // you can pass the default 0 = Base64.DEFAULT
String ba1 = Base64.encodeToString(ba, flag);
// Log.e("base64", "-----" + ba1);
Toast.makeText(AndroidCamera.this,
"New Image saved 22222:" + photoFile, Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(AndroidCamera.this,
UploadActivity.class);
fileUri = Uri.fromFile(pictureFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,
CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
launchUploadActivity(true);
} catch (Exception error) {
// Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename +
// "not saved: "
// + error.getMessage());
Toast.makeText(AndroidCamera.this, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
}
};
您可以像这样将位图旋转 90 度:
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
在你的具体例子中,需要存回原来的路径:
Bitmap bm = BitmapFactory.decodeFile(filename);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
FileOutputStream fos = new FileOutputStream(pictureFile);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();