【问题标题】:How to create a flash light as well as strobe effect. Need help to fix strobe effect如何创建闪光灯和频闪效果。需要帮助修复频闪效果
【发布时间】:2013-07-23 10:25:22
【问题描述】:

我正在尝试创建一个包含频闪功能的闪光灯应用程序。我已经成功实现了闪光灯开/关效果,但我遇到了频闪效果问题。每当我尝试创建频闪效果时,应用程序就会崩溃。如果我删除它,它工作正常。

我有一项活动,顶部是 seekbar,中间是开/关开关。我想要实现的是,如果用户单击开/关按钮,如果顶部的 seekbar 为 0,则闪光灯应该打开/关闭。如果用户增加 seekbar 中的值,闪光灯应该启动进行相应的频闪,如果用户将值移回 0,手电筒应保持不变。请帮我修复错误。

记录猫

07-23 16:54:12.610: W/dalvikvm(21210): threadid=11: thread exiting with uncaught exception (group=0x40c15a68)
07-23 16:54:12.610: E/AndroidRuntime(21210): FATAL EXCEPTION: Thread-2373
07-23 16:54:12.610: E/AndroidRuntime(21210): java.lang.RuntimeException: Fail to connect to camera service
07-23 16:54:12.610: E/AndroidRuntime(21210):    at android.hardware.Camera.native_setup(Native Method)
07-23 16:54:12.610: E/AndroidRuntime(21210):    at android.hardware.Camera.<init>(Camera.java:365)
07-23 16:54:12.610: E/AndroidRuntime(21210):    at android.hardware.Camera.open(Camera.java:340)
07-23 16:54:12.610: E/AndroidRuntime(21210):    at com.shyam.flashlight.StrobeController.run(StrobeController.java:29)
07-23 16:54:12.610: E/AndroidRuntime(21210):    at java.lang.Thread.run(Thread.java:856)

FlashLight.java

public class FlashLight extends Activity {

ImageButton btnSwitch;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;

StrobeController runner;
Thread bw;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_flash_light);

    btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);

    runner = StrobeController.getInstance();

    bw = new Thread(runner);
    bw.start();

    hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        AlertDialog alert = new AlertDialog.Builder(FlashLight.this).create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        alert.show();
        return;
    }

    getCamera();

    toggleButtonImage();

    btnSwitch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            /*if (isFlashOn) {
                turnOffFlash();
            } else {
                turnOnFlash();
            }*/

            if (isFlashOn) {
                bw = new Thread(runner);
                bw.start();
                isFlashOn = false;
            } else {
                isFlashOn = true;
                runner.requestStop = true;
            }

        }
    });

    final SeekBar skbar = (SeekBar) findViewById(R.id.volume_bar);
    skbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) { }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) { }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            runner.delay = 101 - progress;
            runner.delayoff = 101 - progress;
        }
    });

}

private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    }
}

private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;

        toggleButtonImage();
    }
}

private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;

        toggleButtonImage();
    }
}

private void playSound() {
    if (isFlashOn) {
        mp = MediaPlayer.create(FlashLight.this, R.raw.light_switch_off);
    } else {
        mp = MediaPlayer.create(FlashLight.this, R.raw.light_switch_on);
    }
    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
    mp.start();
}

private void toggleButtonImage() {
    if (isFlashOn) {
        btnSwitch.setImageResource(R.drawable.btn_switch_on);
    } else {
        btnSwitch.setImageResource(R.drawable.btn_switch_off);
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashOn = false;

    if (camera != null) {
        camera.release();
        camera = null;
    }
    Log.d("Camera", "Back Pressed");
}

}

StrobeController.java

public class StrobeController implements Runnable {

protected StrobeController() { }

public static StrobeController getInstance() {
    return (instance == null ? instance = new StrobeController() : instance);
}

private static StrobeController instance;
public volatile boolean requestStop = false;
public volatile boolean isRunning = false;
public volatile int delay = 10;
public volatile int delayoff = 500;
public volatile String errorMessage = "";

@Override
public void run() {
    if (isRunning)
        return;

    requestStop = false;
    isRunning = true;

    Camera cam = Camera.open();

    Parameters pon = cam.getParameters(), poff = cam.getParameters();

    pon.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    poff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

    while (!requestStop) {
        try {
            cam.setParameters(pon);
            Thread.sleep(delay);
            cam.setParameters(poff);
            Thread.sleep(delayoff);
        } catch (InterruptedException ex) {

        } catch (RuntimeException ex) {
            requestStop = true;
            errorMessage = "Error setting camera flash status. Your device may be unsupported.";
        }
    }

    cam.release();

    isRunning = false;
    requestStop = false;
}

}

【问题讨论】:

  • 查看'logcat'的输出,它会告诉你异常是在哪里抛出的,或者在这里发布它的输出
  • @Kerry 请参阅编辑。我已经粘贴了 logcat 的输出。

标签: java android flashlight


【解决方案1】:

我注意到第 29 行抛出了异常:

在 com.shyam.flashlight.StrobeController.run(StrobeController.java:29)

第 29 行是Camera cam = Camera.open();吗?

您拥有here? 所述的正确权限 另外,您是否要打开空相机?看看 StrobeController 类中的第 29 行是什么。

【讨论】:

  • 我在清单中添加了以下权限&lt;uses-permission android:name="android.permission.CAMERA"/&gt; &lt;uses-permission android:name="android.hardware.camera"/&gt;
  • StrobeController 中的第 29 行是 Camera cam = Camera.open();
  • 我并没有真正使用过安卓的相机设备,但谷歌搜索似乎这可能是以前没有发布相机的问题?我会用谷歌搜索异常文本并浏览各种答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多