简体   繁体   English

如何创建闪光灯以及频闪效果。 需要帮助修复频闪效果

[英]How to create a flash light as well as strobe effect. Need help to fix strobe effect

I am trying to create an flash light application which contains strobe function. 我正在尝试创建一个包含闪光灯功能的闪光灯应用程序。 I have successfully achieved flash light on/off effect but I am running into trouble with strobe effect. 我已经成功实现了闪光灯开/关效果,但是频闪效果却遇到了麻烦。 Whenever I try to create the strobe effect the application crashes. 每当我尝试创建频闪效果时,应用程序就会崩溃。 If I remove that, it works fine. 如果我删除它,它工作正常。

I have one activity with a seekbar at the top and the on/off switch at the center. 我有一个活动,顶部有一个搜索seekbar ,中间是开/关开关。 What I want to achieve is that if the user clicks on/off button the flash light should toggle on/off if the seekbar at the top is at 0. If the user increase the value in seekbar the flash light should start to strobe accordingly and if the user move the value back to 0 flashlight should remain constant. 我想实现的是,如果用户点击开/关按钮闪光灯指示灯应打开/关闭,如果在seekbar的顶部是0。如果用户在增加值seekbar闪光灯应该启动相应频闪和如果用户将值移回0,则手电筒应保持恒定。 Please help me fix the error. 请帮助我修复错误。

LOG CAT 日志猫

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 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 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;
}

}

I notice the exception is being thrown at line 29: 我注意到第29行抛出了异常:

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

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

You have the correct permissions as described here? 您具有此处所述的正确权限吗? Also, are you trying to open a null camera? 另外,您是否要打开空照相机? Look at what line 29 is in StrobeController class. 查看StrobeController类中的第29行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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