在我的代码中旋转在我的应用程序中捕获的风景图像的正确点是什么? [英] What is the right point in my code to rotate an landscape image captured in my app?
本文介绍了在我的代码中旋转在我的应用程序中捕获的风景图像的正确点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一般来说,我没有太多使用相机和文件的经验. 我已经集成了CameraKit的库来捕获图像,这是我当前的代码:
I don't have much experience working with the camera and files in general. I've integrated CameraKit's library to capture images, and this is my current code:
captureButton.setOnClickListener {
cameraKitView.captureImage() { _, p1 ->
val timeStamp = System.currentTimeMillis().toString()
val fileName = "Dere$timeStamp.jpg"
val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + "Dere"
val outputDir = File(path)
outputDir.mkdir()
val savedPhoto = File(path + File.separator + fileName)
try {
val outputStream = FileOutputStream(savedPhoto.path)
outputStream.write(p1)
outputStream.close()
mActivity.sendBroadcast(
Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(savedPhoto)
)
)
// Here I'm already loading the image into an mage view for the user to apprve the photo
Glide.with(mActivity).load(savedPhoto)
.into(mActivity.photoEditorFragment.view!!.photo_editor_image)
// at this point I save this photo with some extra details that were collected to the local room database
val localImagePost = LocalImagePost(
timeStamp.toLong(),
location.longitude,
location.latitude,
savedPhoto.path,
"",
"",
true
)
localImageViewModel.insert(localImagePost)
sharedViewModelLocalImagePost.sharedImagePostObject.postValue(localImagePost)
} catch (e: java.io.IOException) {
e.printStackTrace()
}
}
}
P1 is a ByteArray. I've asked a question before here Can I get the orientation of a photo taken in my app if I limit the activity's orientation to portrait only in my manifest? but I can't figure out how and where to use it in my code. Do I create a brand new file FROM the first file I just created, and then delete the first one? Or do I just start off by creating the rotated file? I'm a bit lost, would appreciate any help, thanks!
推荐答案
//SOLUTION 1->
//find the Orientation Using ExifInterface
try{
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
//From the orientation value you can convert(Rotate) image according to orientation and can be set it to the ImageView later
int rotate = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
}
catch (Exception e) {
}
// Image rotation //
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);
// SOLUTION 2
ExifInterface oldExif = new ExifInterface(oldImagePath);
String exifOrientation = oldExif.getAttribute(ExifInterface.TAG_ORIENTATION);
if (exifOrientation != null) {
ExifInterface newExif = new ExifInterface(imagePath);
newExif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation);
newExif.saveAttributes();
}
这篇关于在我的代码中旋转在我的应用程序中捕获的风景图像的正确点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文