简体   繁体   中英

Kotlin logic conversion to Java problem with method “contains”

I'm trying to use the following piece of code posted on goo.gl/av3tJD

val supportsDistortionCorrection = characteristics.get(

    CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES)?.contains(

    CameraMetadata.DISTORTION_CORRECTION_MODE_HIGH_QUALITY) ?: false

I see the code is in Kotlin, which I know very little about. I want to convert the code to Java.

I'm trying the following:

Boolean hasNoiseReduction = false;
//cc is my previously defined CameraCharacteristics object
if(cc.get(CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES).contains("some string"))hasNoiseReduction = true;

The method contains is throwing me off. I don't believe is the method contains used in the String class.

Any advice is needed

it.first at that point in the code is referring to the first element of the Pair created above, containing a CameraCharacteristics instance. The get on this returns an IntArray ( int[] in Java terms), and the contains method from the Kotlin standard library checks whether the given constant is in that array.

val cameraCharacteristics: CameraCharacteristics = it.first
val capabilities: IntArray = cameraCharacteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES)!!
capabilities.contains(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA)

You could check this in Java by - for example - looping through the array elements and seeing if you find the value. A primitive implementation for this:

CameraCharacteristics cameraCharacteristics = ...;
final int[] capabilities = cameraCharacteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);

boolean contains = false;
for (final int capability : capabilities) {
    if (capability == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA) {
        contains = true;
        break;
    }
}

// use result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM