@@ -14,10 +14,11 @@ import androidx.camera.core.internal.compat.workaround.ExifRotationAvailability
14
14
import androidx.camera.core.internal.utils.ImageUtil
15
15
import androidx.camera.core.internal.utils.ImageUtil.CodecFailedException
16
16
import androidx.camera.core.internal.utils.ImageUtil.CodecFailedException.FailureType
17
+ import androidx.exifinterface.media.ExifInterface
18
+ import app.grapheneos.camera.clearExif
19
+ import app.grapheneos.camera.fixExif
17
20
import app.grapheneos.camerax.OutputFileOptions.OutputFileOptionsMediaStore
18
- import app.grapheneos.camerax.OutputFileOptions.OutputFileOptionsOutputStream
19
21
import java.io.IOException
20
- import java.io.OutputStream
21
22
import java.io.UnsupportedEncodingException
22
23
import java.util.concurrent.Executor
23
24
import java.util.concurrent.RejectedExecutionException
@@ -51,18 +52,14 @@ class ImageSaver(
51
52
is OutputFileOptionsMediaStore -> {
52
53
val (contentResolver, saveCollection, values) = mOutputFileOptions
53
54
setContentValuePending(values, pending)
54
- val outputUri = contentResolver.insert(
55
- saveCollection,
56
- values
57
- )
58
- val output: OutputStream = contentResolver.openOutputStream(outputUri!! )!!
59
- saveException = output.writeImageAndExif()
55
+ val outputUri = contentResolver.insert(saveCollection, values)!!
56
+ saveException = writeImageAndExif(contentResolver, outputUri)
60
57
setUriNotPending(outputUri, contentResolver)
61
58
uri = outputUri
62
59
}
63
- is OutputFileOptionsOutputStream -> {
64
- val (outputStream ) = mOutputFileOptions
65
- saveException = outputStream. writeImageAndExif()
60
+ is OutputFileOptions . OutputFileOptionsOutputStream -> {
61
+ val (contentResolver, outputUri ) = mOutputFileOptions
62
+ saveException = writeImageAndExif(contentResolver, outputUri )
66
63
}
67
64
}
68
65
} catch (e: IOException ) {
@@ -96,33 +93,38 @@ class ImageSaver(
96
93
val exception : Exception ? = null ,
97
94
)
98
95
99
- private fun OutputStream.writeImageAndExif (): SaveException ? {
96
+ private fun writeImageAndExif (
97
+ contentResolver : ContentResolver , uri : Uri
98
+ ): SaveException ? {
100
99
var saveException: SaveException ? = null
101
100
try {
102
- val bytes = imageToJpegByteArray(mImage, mJpegQuality)
103
- write(bytes)
104
-
105
- // Create new exif based on the original exif.
106
- val exif = Exif .createFromImageProxy(mImage)
107
- Exif .createFromImageProxy(mImage).copyToCroppedImage(exif)
108
-
109
- // Overwrite the original orientation if the quirk exists.
110
- if (! ExifRotationAvailability ().shouldUseExifOrientation(mImage)) {
111
- exif.rotate(mOrientation)
112
- }
113
-
114
- // Overwrite exif based on metadata.
115
101
val metadata = mOutputFileOptions.metadata
116
- if (metadata.isReversedHorizontal) {
117
- exif.flipHorizontally()
118
- }
119
- if (metadata.isReversedVertical) {
120
- exif.flipVertically()
102
+ contentResolver.openOutputStream(uri)?.use { os ->
103
+ val bytes = imageToJpegByteArray(mImage, mJpegQuality)
104
+ os.write(bytes)
121
105
}
122
- if (metadata.location != null ) {
123
- exif.attachLocation(metadata.location!! )
124
- }
125
- exif.save()
106
+
107
+ contentResolver.openAssetFileDescriptor(uri, " rw" )?.createInputStream()
108
+ ?.use { inputStream ->
109
+ val exifSavedImage = Exif .createFromInputStream(inputStream)
110
+ val exifCapturedImage = Exif .createFromImageProxy(mImage)
111
+ exifCapturedImage.copyToCroppedImage(exifSavedImage)
112
+
113
+ metadata.location?.let { exifSavedImage.attachLocation(it) }
114
+ if (metadata.isReversedHorizontal) {
115
+ exifSavedImage.flipHorizontally()
116
+ }
117
+ if (metadata.isReversedVertical) {
118
+ exifSavedImage.flipVertically()
119
+ }
120
+ if (! ExifRotationAvailability ().shouldUseExifOrientation(mImage)) {
121
+ exifSavedImage.rotate(mOrientation)
122
+ }
123
+ exifSavedImage.save()
124
+ }
125
+ contentResolver.openAssetFileDescriptor(uri, " rw" )?.createInputStream()?.use {
126
+ ExifInterface (it).fixExif().clearExif().saveAttributes()
127
+ }
126
128
} catch (e: IOException ) {
127
129
saveException = SaveException (
128
130
SaveError .FILE_IO_FAILED ,
0 commit comments