Can I Use CameraDevice.StateCallback in Rust Without writing Java?

โš“ rust    ๐Ÿ“… 2025-05-17    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 4      

surdeus

Warning

This post was published 44 days ago. The information described in this article may have changed.

Hey!
Iโ€™m trying to make a camera callback in Rust using jni_min_helper and jni. I want to use the CameraDevice$StateCallback but I donโ€™t want to write any Java. So I tried to make a proxy like this

let proxy = match JniProxy::build(
    &mut env,
    None,
    &["android/hardware/camera2/CameraDevice$StateCallback"],
    |_env, _method_obj, _args| {
        println!("proxy callback started");
        Ok(_env.auto_local(JObject::null()))
    },
) {
    Ok(proxy) => proxy,
    Err(e) => {
        eprintln!("error did not build proxy: {:?}", e);
        return Err(e);
    }
};

But it fails and prints error did not build proxy: JavaException and the callback never runs. I donโ€™t know what to do. Is it possible to do this just with Rust and JNI? Or do I have to write Java code?

Also env.find_class did find the class, so I know the API path is right...
class found: JClass(JObject { internal: 0x19, lifetime: PhantomData<&()> })

Not sure if its any help but here is the entire method

    fn camera_callback(&mut self) -> Result<(), jni::errors::Error> {
        if self.camera_state_callback.is_none() {
            let mut env = self.java_vm.attach_current_thread()?;

            let callback_class = env.find_class("android/hardware/camera2/CameraDevice$StateCallback")?;
            println!("class found: {:?}", callback_class);
            //JClass(JObject { internal: 0x19, lifetime: PhantomData<&()> })

            let proxy = match JniProxy::build(
                &mut env,
                None,
                &["android/hardware/camera2/CameraDevice$StateCallback"],
                |_env, _method_obj, _args| {
                    //this never runs
                    println!("proxy callback started");
                    Ok(_env.auto_local(JObject::null())) 
                },
            ) {
                Ok(proxy) => proxy,
                Err(e) => {
                    eprintln!("error did not build proxy: {:?}", e);
                    return Err(e);
                }
            };

            let global = env.new_global_ref(proxy)?;
            self.camera_state_callback = Some(global);
        }
        Ok(())
    }

Thanks for any help!

1 post - 1 participant

Read full topic

๐Ÿท๏ธ rust_feed