1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.camera.integration.uiwidgets.rotations
18 
19 import android.content.ContentResolver
20 import android.net.Uri
21 import android.util.Size
22 import androidx.camera.core.ImageProxy
23 import androidx.camera.core.impl.utils.Exif
24 import java.io.File
25 
26 /** A wrapper around an ImageCapture result, used for testing. */
27 sealed class ImageCaptureResult {
28 
getResolutionnull29     abstract fun getResolution(): Size
30 
31     abstract fun getRotation(): Int
32 
33     abstract fun delete()
34 
35     class InMemory(private val imageProxy: ImageProxy) : ImageCaptureResult() {
36         override fun getResolution() = Size(imageProxy.width, imageProxy.height)
37 
38         override fun getRotation() = imageProxy.imageInfo.rotationDegrees
39 
40         override fun delete() {}
41     }
42 
43     class FileOrOutputStream(private val file: File) : ImageCaptureResult() {
44         private val exif = Exif.createFromFile(file)
45 
getResolutionnull46         override fun getResolution() = Size(exif.width, exif.height)
47 
48         override fun getRotation() = exif.rotation
49 
50         override fun delete() {
51             file.delete()
52         }
53     }
54 
55     class MediaStore(private val contentResolver: ContentResolver, private val uri: Uri) :
56         ImageCaptureResult() {
57 
58         private val exif: Exif
59 
60         init {
61             val inputStream = contentResolver.openInputStream(uri)
62             exif = Exif.createFromInputStream(inputStream!!)
63         }
64 
getResolutionnull65         override fun getResolution() = Size(exif.width, exif.height)
66 
67         override fun getRotation() = exif.rotation
68 
69         override fun delete() {
70             contentResolver.delete(uri, null, null)
71         }
72     }
73 }
74