Reputation: 7856
I am working with Camera2 API, and I am trying to retrieve the largest available image size. I want to use Collections.max. I have tried to write this code in Java, and it works fine.
JAVA Ex-
CameraCharacteristics characteristics
= manager.getCameraCharacteristics(cameraId);
// We don't use a front facing camera in this sample.
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null) {
continue;
}
// For still image captures, we use the largest available size.
Size largest = Collections.max(
Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
new CompareSizesByArea());
However, the Kotlin equivalent has an issue with the Collections.max
operation.
Kotlin Ex-
val characteristics = manager.getCameraCharacteristics(cameraId)
val map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP
) ?: continue
// for still image captures, we use the largest available size
val largest = Collections.max(
listOf(map.getOutputSizes(ImageFormat.JPEG)),
CompareSizesByArea()
)
The error says,
None of the following substitutions ((MutableCollection<out Array<(out) Size!>!>..Collection<Array<(out) Size!>!>?),Comparator<in Array<(out) Size!>!>!) ((MutableCollection<out Size!>..Collection<Size!>?),Comparator<in Size!>!) can be applied to (List<Array<(out) Size!>!>,CompareSizesByArea)
I am not sure how to correct my code. I can use Collections.max in other instances, such as
val test = listOf(1,2,3,4,3,2,1)
Collections.max(test)
The issue is the map of output sizes??
Upvotes: 0
Views: 129
Reputation: 93581
Arrays.asList()
, as you used in your Java code, converts an array to a List
. So you have a List<Size>
.
listOf()
, as you used in your Kotlin code, creates a list of the items you pass to it. In this case, you pass a single item to it, an array. So you are creating a List<Array<Size>>
with a single object in it, the array of sizes.
The equivalent way to do Arrays.asList()
in Kotlin is to simply call .asList()
on the array:
val largest = Collections.max(
map.getOutputSizes(ImageFormat.JPEG).asList(),
CompareSizesByArea()
)
But if you're going to do it purely in Kotlin, you can skip the Collections
function and do this more simply:
val largest = map.getOutputSizes(ImageFormat.JPEG).maxBy { it.width * it.height }
Upvotes: 2