Skip to content

Commit

Permalink
Torch method and orientation
Browse files Browse the repository at this point in the history
Added a method to turn on the camera's flash during the scanning process.
Added some block in to handle the device orientation
  • Loading branch information
micaTewe committed Sep 14, 2015
1 parent 39af9e1 commit 8e3abe3
Showing 1 changed file with 107 additions and 5 deletions.
112 changes: 107 additions & 5 deletions android/ZBarScannerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
Expand Down Expand Up @@ -177,8 +179,8 @@ public void onResume ()
return;
}

Camera.Parameters camParams = camera.getParameters();
if(flashMode.equals("on")) {
/* Camera.Parameters camParams = camera.getParameters();
if(flashMode.equals("on")) {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else if(flashMode.equals("off")) {
camParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
Expand All @@ -194,7 +196,7 @@ public void onResume ()
Log.d("csZBar", "Unsupported camera parameter reported for flash mode: "+flashMode);
}
tryStartPreview();
tryStartPreview();*/
}

@Override
Expand Down Expand Up @@ -251,7 +253,78 @@ public void surfaceChanged (SurfaceHolder hld, int fmt, int w, int h)
holder = hld;
tryStartPreview();
}
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch(rotation)
{
case 0: // '\0'
rotation = 90;
break;

case 1: // '\001'
rotation = 0;
break;

case 2: // '\002'
rotation = 270;
break;

case 3: // '\003'
rotation = 180;
break;

default:
rotation = 90;
break;
}
camera.setDisplayOrientation(rotation);
android.hardware.Camera.Parameters params = camera.getParameters();
tryStopPreview();
tryStartPreview();

}

public void toggleFlash(View view)
{


camera.startPreview();
android.hardware.Camera.Parameters camParams = camera.getParameters();
//If the flash is set to off
if(camParams.getFlashMode().equals(Parameters.FLASH_MODE_OFF) && !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_TORCH))&& !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_ON)))
camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
else //if(camParams.getFlashMode() == Parameters.FLASH_MODE_ON || camParams.getFlashMode()== Parameters.FLASH_MODE_TORCH)
camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
try
{
// camera.setParameters(camParams);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(previewCb);
camera.startPreview();
if (android.os.Build.VERSION.SDK_INT >= 14) {
camera.autoFocus(autoFocusCb); // We are not using any of the
// continuous autofocus modes as that does not seem to work
// well with flash setting of "on"... At least with this
// simple and stupid focus method, we get to turn the flash
// on during autofocus.
camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
//tryStopPreview();
//tryStartPreview();
//camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(camParams);


}
catch(RuntimeException e)
{
Log.d("csZBar", (new StringBuilder("Unsupported camera parameter reported for flash mode: ")).append(flashMode).toString());
} catch (IOException e) {
Log.d("csZBar", (new StringBuilder("Wrong holder data")).append(flashMode).toString());
}
}
// Continuously auto-focus -----------------------------------------
// For API Level < 14

Expand Down Expand Up @@ -380,9 +453,38 @@ public Collection<ZBarcodeFormat> getFormats() {
private void tryStartPreview () {
if(holder != null) {
try {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch(rotation)
{
case 0: // '\0'
rotation = 90;
break;

case 1: // '\001'
rotation = 0;
break;

case 2: // '\002'
rotation = 270;
break;

case 3: // '\003'
rotation = 180;
break;

default:
rotation = 90;
break;
}
// 90 degrees rotation for Portrait orientation Activity.
camera.setDisplayOrientation(90);

camera.setDisplayOrientation(rotation);
android.hardware.Camera.Parameters camParams = camera.getParameters();

//camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);

camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(camParams);

camera.setPreviewDisplay(holder);
camera.setPreviewCallback(previewCb);
camera.startPreview();
Expand Down

0 comments on commit 8e3abe3

Please sign in to comment.