@@ -67,14 +67,9 @@ import coil.request.SuccessResult
6767import com.google.ai.sample.GenerativeViewModelFactory
6868import coil.size.Precision
6969import com.google.ai.sample.R
70- import com.google.ai.sample.ScreenOperatorAccessibilityService
7170import com.google.ai.sample.util.UriSaver
72- import kotlinx.coroutines.delay
7371import kotlinx.coroutines.launch
74- import java.io.File
75- import android.content.Context
7672import android.util.Log
77- import android.widget.Toast
7873
7974@Composable
8075internal fun PhotoReasoningRoute (
@@ -91,71 +86,34 @@ internal fun PhotoReasoningRoute(
9186 uiState = photoReasoningUiState,
9287 onReasonClicked = { inputText, selectedItems ->
9388 coroutineScope.launch {
94- Log .d(" PhotoReasoningScreen" , " Go button clicked, taking screenshot " )
89+ Log .d(" PhotoReasoningScreen" , " Go button clicked, processing images " )
9590
96- // Show a toast to indicate we're taking a screenshot
97- Toast .makeText(context, " Taking screenshot..." , Toast .LENGTH_SHORT ).show()
98-
99- // Take a screenshot first
100- ScreenOperatorAccessibilityService .takeScreenshot {
101- // This will be called after the screenshot is taken
102- coroutineScope.launch {
103- Log .d(" PhotoReasoningScreen" , " Screenshot callback triggered" )
104-
105- // Give some time for the screenshot to be saved and processed
106- delay(1000 )
107-
108- // Get the latest screenshot URI directly
109- val screenshotUri = ScreenOperatorAccessibilityService .getLatestScreenshotUri()
110- val updatedItems = selectedItems.toMutableList()
111-
112- // Add the screenshot to the list if it exists
113- if (screenshotUri != null ) {
114- try {
115- updatedItems.add(screenshotUri)
116- Log .d(" PhotoReasoningScreen" , " Added screenshot URI: $screenshotUri " )
117-
118- // Show a toast to indicate the screenshot was added
119- Toast .makeText(context, " Screenshot added" , Toast .LENGTH_SHORT ).show()
120- } catch (e: Exception ) {
121- Log .e(" PhotoReasoningScreen" , " Error adding screenshot URI: ${e.message} " )
122- Toast .makeText(context, " Error adding screenshot: ${e.message} " , Toast .LENGTH_SHORT ).show()
123- }
91+ // Process all selected images
92+ val bitmaps = selectedItems.mapNotNull {
93+ Log .d(" PhotoReasoningScreen" , " Processing image: $it " )
94+ val imageRequest = imageRequestBuilder
95+ .data(it)
96+ .precision(Precision .EXACT )
97+ .build()
98+ try {
99+ val result = imageLoader.execute(imageRequest)
100+ if (result is SuccessResult ) {
101+ Log .d(" PhotoReasoningScreen" , " Successfully processed image" )
102+ return @mapNotNull (result.drawable as BitmapDrawable ).bitmap
124103 } else {
125- Log .e(" PhotoReasoningScreen" , " Screenshot URI not found" )
126- Toast .makeText(context, " Screenshot not found" , Toast .LENGTH_SHORT ).show()
127- }
128-
129- // Process all images including the screenshot
130- val bitmaps = updatedItems.mapNotNull {
131- Log .d(" PhotoReasoningScreen" , " Processing image: $it " )
132- val imageRequest = imageRequestBuilder
133- .data(it)
134- // Scale the image down to 768px for faster uploads deaktiviert um genaue Auflösungen feedback zu bekommen
135- // .size(size = 768)
136- .precision(Precision .EXACT )
137- .build()
138- try {
139- val result = imageLoader.execute(imageRequest)
140- if (result is SuccessResult ) {
141- Log .d(" PhotoReasoningScreen" , " Successfully processed image" )
142- return @mapNotNull (result.drawable as BitmapDrawable ).bitmap
143- } else {
144- Log .e(" PhotoReasoningScreen" , " Failed to process image: result is not SuccessResult" )
145- return @mapNotNull null
146- }
147- } catch (e: Exception ) {
148- Log .e(" PhotoReasoningScreen" , " Error processing image: ${e.message} " )
149- return @mapNotNull null
150- }
104+ Log .e(" PhotoReasoningScreen" , " Failed to process image: result is not SuccessResult" )
105+ return @mapNotNull null
151106 }
152-
153- Log .d(" PhotoReasoningScreen" , " Processed ${bitmaps.size} images" )
154-
155- // Send to AI
156- viewModel.reason(inputText, bitmaps)
107+ } catch (e: Exception ) {
108+ Log .e(" PhotoReasoningScreen" , " Error processing image: ${e.message} " )
109+ return @mapNotNull null
157110 }
158111 }
112+
113+ Log .d(" PhotoReasoningScreen" , " Processed ${bitmaps.size} images" )
114+
115+ // Send to AI
116+ viewModel.reason(inputText, bitmaps)
159117 }
160118 }
161119 )
0 commit comments