Hi Friends,
The reason why I write you is my wish to have the zxing fragment library expanded with a flashlight function.Below you may see my code that generates a flashlight.
My problem is now that I need a camera opener function.
Unfortunately the code doesn´t work due to the fact that the camera is already open. And so there is a conflict meaning that the camera can´t open twice, because the first camera-open-function was used for opening the bar code scanner, and so I get an error message when I try to open the camera the second time.
Now, how do I handle this situation?
Is it possible through the library to get access to switch on and put off function for the flashlight. Will you be kind enough to help me?
Exceptions:
E/Camera Error. Failed to Open. Error:(28639): Fail to connect to camera service
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.(Camera.java:413)
at android.hardware.Camera.open(Camera.java:384)
at dk.logoslogit.repository.ZxingScanFragment.getCamera(ZxingScanFragment.java:225)
at dk.logoslogit.repository.ZxingScanFragment.access$1(ZxingScanFragment.java:222)
at dk.logoslogit.repository.ZxingScanFragment$2.onClick(ZxingScanFragment.java:79)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
public class ZxingScanFragment extends Fragment {
private final String TAG = "ZxingScanFragment";
private FragmentTransaction fTransaction;
private Camera camera;
private Fragment fDisplay;
private ImageButton torch;
private boolean isFlashOn;
private boolean hasFlash;
private Parameters parameter;
private MediaPlayer player;
private View mView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
super.onCreateView(inflater, container, savedInstance);
mView = inflater.inflate(R.layout.scan_zxing, container, false);
setListTitle();
torch = (ImageButton) mView.findViewById(R.id.torch);
// First check if device is supporting flashlight or not
hasFlash = getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// Device doesn't support flash
// Show alert message and close the application
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Error");
alertDialog.setMessage("Sorry, your device doesn't support flash light!");
alertDialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Closing the application
dialog.cancel();
}
});
alertDialog.show();
}
// Displaying button image
toggleButtonImage();
// Switch button click event to toggle flash on/off
torch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
// On starting the app get the camera params
getCamera();
// Turn off flash
turnOffFlash();
} else {
// On starting the app get the camera params
getCamera();
// Turn on flash
turnOnFlash();
}
}
});
if (savedInstance == null) {
fDisplay = new ScanFragment();
fTransaction = getChildFragmentManager().beginTransaction();
fTransaction.remove(fDisplay);
fTransaction.replace(R.id.scan_layout, fDisplay);
fTransaction.commit();
}
return mView;
}
private void setListTitle() {
try {
TextView onlineStatus = (TextView) mView.findViewById(R.id.list_title);
onlineStatus.setText("Scan stregkode");
} catch (Exception ex) {
Log.v(TAG, "Exception: setListTitle");
ex.printStackTrace();
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || parameter == null) {
return;
}
// Play sound
playSound();
parameter = camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameter);
camera.startPreview();
isFlashOn = true;
// Changing button/switch image
toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || parameter == null) {
return;
}
// Play sound
playSound();
parameter = camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameter);
camera.stopPreview();
isFlashOn = false;
// Changing button/switch image
toggleButtonImage();
}
}
// Playing sound
// will play button toggle sound on flash on / off
private void playSound(){
if(isFlashOn) {
player = MediaPlayer.create(getActivity(), R.raw.light_switch_off);
} else {
player = MediaPlayer.create(getActivity(), R.raw.light_switch_on);
}
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
player.release();
}
});
player.start();
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
if(isFlashOn) {
torch.setImageResource(R.drawable.scan_icon);
} else {
torch.setImageResource(R.drawable.room_icon);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
@Override
public void onPause() {
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
// On pause turn off the flash
turnOffFlash();
}
@Override
public void onResume() {
super.onResume();
// On resume turn off the flash
if(hasFlash) {
turnOffFlash();
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
// On stop release the camera
if (camera != null) {
camera.release();
camera = null;
}
}
// Get the camera
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
parameter = camera.getParameters();
} catch (RuntimeException re) {
Log.e("Camera Error. Failed to Open. Error: ", re.getMessage());
re.printStackTrace();
} catch (Exception ex) {
Log.e(TAG, "Exception: getCamera");
Log.e(TAG, ex.getMessage());
ex.printStackTrace();
}
}
}
Hi Friends, The reason why I write you is my wish to have the zxing fragment library expanded with a flashlight function.Below you may see my code that generates a flashlight.
My problem is now that I need a camera opener function. Unfortunately the code doesn´t work due to the fact that the camera is already open. And so there is a conflict meaning that the camera can´t open twice, because the first camera-open-function was used for opening the bar code scanner, and so I get an error message when I try to open the camera the second time.
Now, how do I handle this situation?
Is it possible through the library to get access to switch on and put off function for the flashlight. Will you be kind enough to help me?
Exceptions: E/Camera Error. Failed to Open. Error:(28639): Fail to connect to camera service java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.native_setup(Native Method) at android.hardware.Camera.(Camera.java:413)
at android.hardware.Camera.open(Camera.java:384)
at dk.logoslogit.repository.ZxingScanFragment.getCamera(ZxingScanFragment.java:225)
at dk.logoslogit.repository.ZxingScanFragment.access$1(ZxingScanFragment.java:222)
at dk.logoslogit.repository.ZxingScanFragment$2.onClick(ZxingScanFragment.java:79)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
public class ZxingScanFragment extends Fragment {