Skip to content

Instantly share code, notes, and snippets.

@GOROman
Last active February 24, 2024 14:55
Show Gist options
  • Save GOROman/d403d39b34d7f99934fd to your computer and use it in GitHub Desktop.
Save GOROman/d403d39b34d7f99934fd to your computer and use it in GitHub Desktop.
// UnityからAndroidのCamera LEDを制御する例
// startPreview() でカメラのプレビューを開始した状態で LEDOn() で光る
public class AndroidCamera {
AndroidJavaObject camera = null;
public AndroidCamera() {
WebCamDevice[] devices = WebCamTexture.devices;
Debug.Log("Camera Name:"+devices[0].name);
open();
}
public void open()
{
if (camera == null) {
#if (UNITY_ANDROID && !UNITY_EDITOR)
AndroidJavaClass cameraClass = new AndroidJavaClass("android.hardware.Camera");
camera = cameraClass.CallStatic<AndroidJavaObject>("open");
#endif
}
}
public void release()
{
if (camera != null) {
LEDOff();
camera.Call("release");
camera = null;
}
}
public void startPreview()
{
if (camera != null) {
Debug.Log("AndroidCamera::startPreview()");
camera.Call("startPreview");
}
}
public void stopPreview()
{
if (camera != null) {
Debug.Log("AndroidCamera::stopPreview()");
LEDOff();
camera.Call("stopPreview");
}
}
// Flashモードの設定
private void setFlashMode(string mode)
{
if (camera != null) {
AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");
cameraParameters.Call("setFlashMode",mode);
camera.Call("setParameters",cameraParameters);
}
}
// LED点灯
public void LEDOn() {
setFlashMode("torch");
}
// LED消灯
public void LEDOff() {
setFlashMode("off");
}
}
@Jeremy-Zheng
Copy link

Hello It is not working. Do you know the reason?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment