onPictureTaken function the light is automatically go down - android

I've run into such a problem. The problem is that when I use onPictureTaken function the light is automatically go down. What should I do with it? I want the light not to turn off.
Photo :
if (v == shot)
{
camera.takePicture (null, null, null, this);
}
flashlight:
if (prefs.getBoolean (getString (R.string.torch), false))
{
Parameters params = camera.getParameters ();
params.setFlashMode (Parameters.FLASH_MODE_TORCH);
camera.setParameters (params);
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
camera.setPreviewCallback(this);
camera.setPreviewDisplay(holder);
Camera.Parameters p = camera.getParameters();
p.setPictureSize(1024, 768);
p.setPictureFormat (PixelFormat.RGB_565);
camera.setParameters(p);
}
catch (IOException e)
{
e.printStackTrace();
}
LayoutParams lp = preview.getLayoutParams();
if (this.getResources().getConfiguration().orientation != Configuration.UI_MODE_TYPE_CAR)
{
camera.setDisplayOrientation(90);
}
else
{
camera.setDisplayOrientation(0);
}
preview.setLayoutParams(lp);
camera.startPreview();
return;
}
#Override
protected void onPause()
{
if (camera != null)
{
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
super.onPause();
return ;
}
protected void onResume()
{
super.onResume();
camera = Camera.open();
}

Related

android camera application flash

I am facing a problem in a custom camera application. With the flash function turned ON, the phone takes the 1st photo with flash, but on the 2nd photo it doesn't use the flash.
flashCameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPressed) {
flashCameraButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.onflash));
flashOnButton();
} else if (isPressed) {
flashCameraButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.offflash));
isPressed = !isPressed;
flashOffButton();
} else
flashCameraButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.onflash));
flashOnButton();
}
});
private void flashOnButton() {
if (camera != null) {
try {
Camera.Parameters param = camera.getParameters();
param.setFlashMode(!flashmode ? Camera.Parameters.FLASH_MODE_ON
: Camera.Parameters.FLASH_MODE_ON);
camera.setParameters(param);
flashmode = !flashmode;
} catch (Exception e) {
// TODO: handle exception
}
}
}
private void flashOffButton() {
if (camera != null) {
try {
Camera.Parameters param = camera.getParameters();
param.setFlashMode(!flashmode ? Camera.Parameters.FLASH_MODE_OFF
: Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
flashmode = !flashmode;
} catch (Exception e) {
// TODO: handle exception
}
}
}
Take a look at the code below. You can do something similar to it:
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
//change the picture
turnOffFlash();
} else {
// turn on flash
//change the picture
turnOnFlash();
}
}
});
}
// Get the camera
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());
}
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
}
// Turning Off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
}
}
What we are doing here is checking if the Flash is ON or OFF, if it is ON, we call the method turnOffFlash() to turn it off and if it is OFF we call the method turnOnFlash() to turn it on.

Android Surface Camera Preview crash after onPause/onBackPressed

Im trying to set up a custom SurfaceView (which work as excepted) for the first time.
After onResume/onPause has been called im getting a "media server died" message.
After the media server died i can add another preview. Any idea why it's crashing? Im Releasing the Camera in onPause and open the camera on onResume.
-- Camera Activity --
public class CameraActivity extends DashboardActivity {
private static String TAG = "CameraActivity";
Camera camera;
CameraPreview cameraPreview;
Context _context;
FrameLayout frameLayout;
CameraPreview preview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
this._context = this;
frameLayout = (FrameLayout) findViewById(R.id.preview);
}
#Override
protected void onResume() {
super.onResume();
camera = Camera.open();
if (null != camera) {
camera.setDisplayOrientation(90);
preview = new CameraPreview(getApplicationContext(), camera);
frameLayout.addView(preview);
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
if (camera != null) {
Log.e(TAG, "Camera was not null on back pressed!");
try {
camera.stopPreview();
} catch (Exception e) {
}
// camera.release();
// camera = null;
}
finish();
}
#Override
protected void onPause() {
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
frameLayout.removeView(preview);
preview = null;
}
-- Camera Preview --
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
CameraPreview(Context context, Camera camera) {
super(context);
this.camera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setFormat(PixelFormat.RGB_332);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
Camera.Parameters p = camera.getParameters();
p.setPictureSize(80, 60);
p.setColorEffect(android.hardware.Camera.Parameters.EFFECT_NONE);
p.setJpegQuality(20);
p.setPreviewFrameRate(1);
p.setPreviewFpsRange(5, 10);
p.setPreviewSize(80, 60);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
}
});
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopCameraPreview() {
}
public void takePicture() {
camera.takePicture(null, null, null);
stopCameraPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "SurfaceDestroyed called");
if (camera != null) {
try {
camera.stopPreview();
} catch (Exception e) {
}
// camera.lock();
// camera.release();
// camera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.d(TAG, "SurfaceChanged called!");
if (holder.getSurface() == null) {
Log.d(TAG, "SurfaceChanged Surface is null! Stopping!");
return;
}
try {
Camera.Parameters parameters = camera.getParameters();
// parameters.setPreviewSize(80, 60);
camera.setParameters(parameters);
// camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The proper way to stop the camera is in
public void onPause() {
super.onPause();
releaseCamera();
}
and
public void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}

android startVideo stretched and in landscape mode

I have some problem in my code. I must start camera and capture it , so here is my code, and my problem is that camera starting with some strange view, like landscape mode and stretched.
mVideoCaptureView = (SurfaceView) findViewById(R.id.cameraView);
SurfaceHolder videoCaptureViewHolder = mVideoCaptureView.getHolder();
videoCaptureViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
videoCaptureViewHolder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceCreated(SurfaceHolder holder) {
startVideo();
}
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
and the startVideo() function
private void startVideo() {
if(mCamera != null) return;
SurfaceHolder videoCaptureViewHolder = null;
try {
mCamera = Camera.open();
} catch (RuntimeException e) {
Log.e("CameraTest", "Camera Open filed");
return;
}
mCamera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera camera) {
}
});
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewFrameRate(30);
mCamera.setParameters(parameters);
if (null != mVideoCaptureView)
videoCaptureViewHolder = mVideoCaptureView.getHolder();
try {
mCamera.setPreviewDisplay(videoCaptureViewHolder);
} catch (Throwable t) {
}
try {
mCamera.startPreview();
} catch (Throwable e) {
mCamera.release();
mCamera = null;
return;
}
}
Also I must add my own buttons , ImageButtons in camera view
Thanks
Regards
sorry for English
There was some function for Camera `camera.setDisplayOrientation(90); I got it from here

app hanging on camera.release()

I'm working on a flashlight app using the camera flash. It seems to work fine but on occasion calling camera.release() causes a hang for about a minute or so. I've included the code below. I've looked at a bunch of examples and I don't see anything that could cause such a thing. Any ideas?
//latest
public void setOn(boolean on, Context context) {
if (lock) {
Log.i(TAG, "lock: true");
return;
}
if (on) {
if (mCamera == null) {
mCamera = Camera.open();
}
Parameters params = mCamera.getParameters();
params.setFlashMode(MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
} else {
if (mCamera != null) {
try {
Parameters params = mCamera.getParameters();
params.setFlashMode(MODE_OFF);
mCamera.setParameters(params);
} finally {
new Thread(new Runnable() {
public void run() {
Log.i(TAG, "new Thread - start");
lock = true;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
lock = false;
Log.i(TAG, "new Thread - end");
}
}).start();
}
}
}
}
//original
public void setOn(boolean on, Context context) {
Camera camera = mCamera;
if (on) {
if (camera == null) {
mCamera = camera = Camera.open();
}
Parameters params = camera.getParameters();
params.setFlashMode(MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
} else {
if (camera != null) {
try {
Parameters params = camera.getParameters();
params.setFlashMode(MODE_OFF);
camera.setParameters(params);
} finally {
camera.stopPreview();
camera.release();
mCamera = camera = null;
}
}
}
}
Try putting it in a thread to run in the background so it wont hang up the UI.
new Thread(new Runnable(){
public void run(){
camera.setPreviewCallback(null); // PreviewCallback de_init.
camera.stopPreview(); // stop Preview
camera.release();
}
}).start();
For me, the solution that works is:
Try{
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
} catch (Exception e){
//...
}
I`ve solved this isuue adding camera.unlock() before release()
camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;
test on more devices needed...
just call your methods like
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
you have to call setPreviewCallback(null) betweeen stopPreview and camera.releass

switch back/front camera on fly

If I use only CAMERA_FACING_BACK or CAMERA_FACING_FRONT all works fine.
I have trouble with switch from CAMERA_FACING_BACK to CAMERA_FACING_FRONT.
My code snippet:
public class PhotoCameraActivity extends Activity implements OnClickListener {
private SurfaceView cameraView;
private Button turnButton;
private Camera camera = null;
private Callback listener;
private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_camera_main);
prepareActivity();
}
private void prepareActivity() {
cameraView = (SurfaceView) findViewById(R.id.photo_camera_surface_view);
turnButton = (ImageButton) findViewById(R.id.turn_button);
turnButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.equals(turnButton)) {
if (Camera.getNumberOfCameras() > 1 && camId < Camera.getNumberOfCameras() - 1) {
startCamera(camId + 1);
} else {
startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
}
}
}
#Override
protected void onResume() {
startCamera(camId);
super.onResume();
}
#Override
protected void onPause() {
stopCamera();
super.onPause();
}
private void startCamera(int cameraId) {
if (camera != null) {
stopCamera();
}
holder = cameraView.getHolder();
listener = new Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
};
holder.addCallback(listener);
camId = cameraId;
camera = Camera.open(cameraId);
Camera.Parameters params = camera.getParameters();
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
params.setPreviewSize(1280, 800);
} else {
params.setPreviewSize(640, 480);
}
camera.setParameters(params);
}
private void stopCamera(){
System.out.println("stopCamera method");
if (camera != null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
holder.removeCallback(listener);
holder = null;
}
}
}
In onCreate() of my activity I add the following onClick listener to a button overlayed on my Preview SurfaceView (there are numerous example on the web for previewing):
ImageButton useOtherCamera = (ImageButton) findViewById(R.id.useOtherCamera);
//if phone has only one camera, hide "switch camera" button
if(Camera.getNumberOfCameras() == 1){
useOtherCamera.setVisibility(View.INVISIBLE);
}
else {
useOtherCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (inPreview) {
camera.stopPreview();
}
//NB: if you don't release the current camera before switching, you app will crash
camera.release();
//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);
//Code snippet for this method from somewhere on android developers, i forget where
setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {
//this step is critical or preview on new camera will no know where to render to
camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
On my test device the back camera has an ID of 0 and the front has an id of 1. I suggest using Camera.CameraInfo static variables for your camera id's rather than hard-coding values. I am sure that will only cause issues on other devices.
I restart Activity with cameraId = 2 and this is working.
I removed and re-added SurfaceView in my layout and so it worked.
public void switchCamera(int cameraType)
{
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT))
{
if(Camera.getNumberOfCameras() > cameraType)
{
// Set selected camera
this.cameraType = cameraType;
}
else
{
// Set default camera (Rear)
this.cameraType = Camera.CameraInfo.CAMERA_FACING_BACK;
}
if(mCamera != null)
{
// Destroy previuos Holder
surfaceDestroyed(holder);
holder.removeCallback(this);
// Remove and re-Add SurfaceView
ViewGroup rootLayout = (ViewGroup) surface.getParent();
rootLayout.removeView(surface);
surface = new SurfaceView(context);
holder = surface.getHolder();
holder.addCallback(this);
rootLayout.addView(surface, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
I have to remove the surface preview before adding a new one:
if (mPreview != null) {
mPreview.surfaceDestroyed(mPreview.getHolder());
mPreview.getHolder().removeCallback(mPreview);
mPreview.destroyDrawingCache();
FrameLayout preview = (FrameLayout) view.findViewById(R.id.camera_frame);
preview.removeView(mPreview);
mPreview.mCamera = null;
mPreview = null;
}
//then add your preview
Use this code:
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
//swap the id of the camera to be used
if (currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK)
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
else
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
try {
mCamera = Camera.open(currentCameraId);
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}
catch (Exception e) { e.printStackTrace(); }
try using this:
camera.stopPreview();
if(camera != null)
camera.release();
try {
camera = Camera.open(/*new id*/);
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception ex) { ex.printStackTrace(); }

Categories

Resources