package tinyClark.util.flashlite;
import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Master extends Activity {
String TAG = "tinyClark";
boolean mIsFlashOpen;
Camera mCamera;
// Camera.Parameters CameraInfo;
Button btn1;
Boolean lightToggle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(lightToggle == false){
turnLightOn();
lightToggle = true;
}else{
turnLightOff();
lightToggle = false;
}
}
});
}
private void turnLightOn() {
if (false == mIsFlashOpen) {
if (null == mCamera) {
try {
Log.e(TAG, "open");
mCamera =
Camera.open(CameraInfo.CAMERA_FACING_BACK);
} catch (RuntimeException e) {
try {
Thread.sleep(500);
Log.e(TAG, "second open");
mCamera =
Camera.open(CameraInfo.CAMERA_FACING_BACK);
} catch (Exception sube) {
Log.e(TAG, "fail to open
camera");
sube.printStackTrace();
mCamera = null;
}
}
}
if (null != mCamera) {
Parameters parameters = mCamera.getParameters();
if (parameters == null) {
return;
}
List<String> flashModes =
parameters.getSupportedFlashModes();
// Check if camera flash exists
if (flashModes == null) {
return;
}
String flashMode = parameters.getFlashMode();
if
(!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
// Turn on the flash
if
(flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.startPreview();
mCamera.setParameters(parameters);
// mRightBtn.setEnabled(true);
} else {
Log.e(TAG, "FLASH_MODE_TORCH
not supported");
}
}
}
}
}
private void turnLightOff() {
if (mIsFlashOpen) {
mIsFlashOpen = false;
if (mCamera == null) {
return;
}
Parameters parameters = mCamera.getParameters();
if (parameters == null) {
return;
}
List<String> flashModes =
parameters.getSupportedFlashModes();
String flashMode = parameters.getFlashMode();
// Check if camera flash exists
if (flashModes == null) {
return;
}
if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {
// Turn off the flash
if
(flashModes.contains(Parameters.FLASH_MODE_OFF)) {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
mCamera.stopPreview();
} else {
Log.e(TAG, "FLASH_MODE_OFF not
supported");
}
}
}
}
}
That is the full source, and the layout is one big button named btn1.
On Feb 8, 12:37 am, Raghav Sood <[email protected]>
wrote:
> The camera can only be used by one application at a time. You must release
> it when you are done with it, so that you may use it again. The other
> applications can still use the camera as your app force closes, which frees
> up the camera.
>
> Thanks
>
> 2012/2/8 v bobbywink <[email protected]>
>
>
>
>
>
>
>
>
>
> > So strange, can u post all code in this activity?
>
> > 2012/2/8 Matt Clark <[email protected]>
>
> >> Even using your code, in LogCat i get:
> >> open
> >> second open
> >> fail to open camera
> >> This happens even after rebooting the phone, however all other
> >> applications that use the camera still work.
>
> >> On Feb 7, 2:45 am, v bobbywink <[email protected]> wrote:
> >> > well, i think camera has crashed and in the condition u can't get
> >> > camera service. U must reboot phone.
> >> > when using hardware , u should be careful. u can open Camera in
> >> > onStart() and release Camera in onStop(). Can't open camera many times
> >> > in activity, when task finish u must release it .
> >> > sample code
> >> > private void turnLightOn() {
> >> > if (false == mIsFlashOpen) {
> >> > if (null == mCamera) {
> >> > try {
> >> > Log.e(TAG, "open");
> >> > mCamera =Camera.open(CameraInfo.CAMERA_FACING_BACK);
> >> > } catch (RuntimeException e) {
> >> > try {
> >> > Thread.sleep(500);
> >> > Log.e(TAG, "second open");
> >> > mCamera
> >> =Camera.open(CameraInfo.CAMERA_FACING_BACK);
> >> > } catch (Exception sube) {
> >> > Log.e(TAG, "fail to open camera");
> >> > sube.printStackTrace();
> >> > mCamera = null;
> >> > }
> >> > }
> >> > }
> >> > if (null != mCamera) {
> >> > Parameters parameters = mCamera.getParameters();
> >> > if (parameters == null) {
> >> > return;
> >> > }
> >> > List<String> flashModes =
> >> > parameters.getSupportedFlashModes();
> >> > // Check if camera flash exists
> >> > if (flashModes == null) {
> >> > return;
> >> > }
> >> > String flashMode = parameters.getFlashMode();
> >> > if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
> >> > // Turn on the flash
> >> > if
> >> > (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
>
> >> > parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
> >> > mCamera.startPreview();
> >> > mCamera.setParameters(parameters);
> >> > mRightBtn.setEnabled(true);
> >> > } else {
> >> > Log.e(TAG, "FLASH_MODE_TORCH not supported");
> >> > }
> >> > }
> >> > }
> >> > }
> >> > }
>
> >> > private void turnLightOff() {
> >> > if (mIsFlashOpen) {
> >> > mIsFlashOpen = false;
> >> > if (mCamera == null) {
> >> > return;
> >> > }
> >> > Parameters parameters = mCamera.getParameters();
> >> > if (parameters == null) {
> >> > return;
> >> > }
> >> > List<String> flashModes =
> >> > parameters.getSupportedFlashModes();
> >> > String flashMode = parameters.getFlashMode();
> >> > // Check if camera flash exists
> >> > if (flashModes == null) {
> >> > return;
> >> > }
> >> > if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {
> >> > // Turn off the flash
> >> > if (flashModes.contains(Parameters.FLASH_MODE_OFF)) {
>
> >> > parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
> >> > mCamera.setParameters(parameters);
> >> > mCamera.stopPreview();
> >> > } else {
> >> > Log.e(TAG, "FLASH_MODE_OFF not supported");
> >> > }
> >> > }
> >> > }
> >> > }
>
> >> > On 2月7日, 下午1时14分, Matt Clark <[email protected]> wrote:
>
> >> > > Even as hundreds of other have made them, i am trying my hand at a
> >> > > flashlight app. I have the basic GUI and widget working and toggling,
> >> > > and i just attempted to implement the actual flash from the phone.
> >> > > I have found many places that this is most easily done with this
> >> > > snippet of code:
>
> >> > > Camera mCamera =Camera.open();
> >> > > Parameters params = mCamera.getParameters();
> >> > > params.setFlashMode( Parameters.FLASH_MODE_TORCH );
> >> > > mCamera.setParameters( params );
>
> >> > > I have tried this code, and every time i run the app it crashes,
> >> > > giving me an output in LogCat of the following:
>
> >> > > 02-07 00:04:49.130: E/AndroidRuntime(4861): Caused by:
> >> > > java.lang.RuntimeException: Fail to connect to camera service
>
> >> > > Every other app that uses the camera or flash works fine, and i have
> >> > > no idea what is causing it to fail.
>
> >> > > In my android manifest i have:
>
> >> > > <uses-permission android:name="android.permission.FLASHLIGHT" />
> >> > > <uses-parmission android:name="android.permission.CAMERA" />
> >> > > <uses-feature android:name="android.hardware.camera" />
> >> > > <uses-feature android:name="android.hardware.camera.flash" />
>
> >> > > Any and all help is greatly appreciated as I am completely stuck after
> >> > > sitting at this for the last several hours...
> >> > > Thanks
> >> > > ~Matt
>
> >> --
> >> You received this message because you are subscribed to the Google
> >> Groups "Android Developers" group.
> >> To post to this group, send email to [email protected]
> >> To unsubscribe from this group, send email to
> >> [email protected]
> >> For more options, visit this group at
> >>http://groups.google.com/group/android-developers?hl=en
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Raghav Soodhttp://www.androidactivist.org/- Authorhttp://www.appaholics.in/-
> Founder
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en