Android 在我的代码中,旋转在我的应用程序中捕获的横向图像的正确点是什么?
一般来说,我没有太多使用相机和文件的经验。 我集成了CameraKit的库来捕获图像,这是我当前的代码:Android 在我的代码中,旋转在我的应用程序中捕获的横向图像的正确点是什么?,android,image,bitmap,camera,android-file,Android,Image,Bitmap,Camera,Android File,一般来说,我没有太多使用相机和文件的经验。 我集成了CameraKit的库来捕获图像,这是我当前的代码: captureButton.setOnClickListener { cameraKitView.captureImage() { _, p1 -> val timeStamp = System.currentTimeMillis().toString() val fileName = "Der
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是一个旁路。
我以前在这里问过一个问题,但我不知道如何以及在我的代码中使用它。我是否从刚创建的第一个文件创建一个全新的文件,然后删除第一个文件?还是从创建旋转文件开始?
我有点迷路了,非常感谢您的帮助,谢谢 谢谢你的回答,Arwy,但它与你给我上一个问题的答案相同,我在这里附上了这个问题。我的问题是,我不知道在哪里以及如何使用您的答案中的代码。我试过了,但没能成功。看看我在这里附上的代码,你能帮我弄明白吗?谢谢你的回答,Arwy,但它与你在我前面的问题中给出的答案相同,我在这里附上了这个问题。我的问题是,我不知道在哪里以及如何使用您的答案中的代码。我试过了,但没能成功。看看我附加在这里的代码,你能帮我弄明白吗?
//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();
}