I'm getting an error of a NullPointerException and it is referencing the line Camera.Parameters:
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
// TODO Auto-generated method stub
Log.e(TAG, "surfaceChanged");
if (mPreviewRunning) {
mCamera.stopPreview();
mPreviewRunning = false;
Log.e(TAG, "stopPeview");
}
Camera.Parameters p = mCamera.getParameters();
Log.e(TAG, "paarameters");
p.setPreviewSize(w, h);
mCamera.setParameters(p);
Log.e(TAG, " set parameters");
try {
mCamera.setPreviewDisplay(holder);
Log.e(TAG, "setPreview");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
Could anyone tell me what the error is in the above code.
I will have to test it again to get the log, but here is the entire code. I did alter the manifest.
public class Picture extends Activity implements SurfaceHolder.Callback{
private String TAG;
private LayoutInflater mInflater = null;
private Camera mCamera;
boolean mPreviewRunning = false;
private SurfaceHolder mSurfaceHolder;
private SurfaceView mSurfaceView;
Button takepicture;
byte[] tempdata;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.camera_surface);
mCamera = getCameraInstance ();
mSurfaceView = (SurfaceView)findViewById(R.id.surface_camera);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mInflater = LayoutInflater.from(this);
View overView = mInflater.inflate(R.layout.cameraoverlay, null);
this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
takepicture = (Button) findViewById(R.id.button);
takepicture.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
mCamera.takePicture(shutterCallback, mPictureCallback,
jpegCallback);
}
});
}
private Camera getCameraInstance() {
// TODO Auto-generated method stub
Camera mCamera = null;
try {
mCamera = Camera.open();
} catch (Exception e) {
}
return mCamera;
}
ShutterCallback shutterCallback = new ShutterCallback() {
#Override
public void onShutter() {}
};
PictureCallback mPictureCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
if(data != null) {
tempdata=data;
done();
}
}
};
void done(){
Bitmap bm = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length);
String url = Images.Media.insertImage(getContentResolver(),
bm, null, null);
bm.recycle();
Bundle bundle = new Bundle();
if(url!=null) {
bundle.putString("url", url);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
} else {
Toast.makeText(this, "Picture can not be saved",
Toast.LENGTH_SHORT).show();
}
finish();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
// TODO Auto-generated method stub
Log.e(TAG, "surfaceChanged");
if (mPreviewRunning) {
mCamera.stopPreview();
mPreviewRunning = false;
Log.e(TAG, "stopPeview");
}
Camera.Parameters p = mCamera.getParameters();
Log.e(TAG, "paarameters");
p.setPreviewSize(w, h);
mCamera.setParameters(p);
Log.e(TAG, " set parameters");
try {
mCamera.setPreviewDisplay(holder);
Log.e(TAG, "setPreview");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e(TAG, "surfaceCreated");
try {
mCamera = Camera.open();
Log.e(TAG, "camera open");
} catch(Exception e) {}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
Also, the line the log referenced was the line: Camera.Parameters p = mCamera.getParameters();
Looks like mCamera is null... did you initialize it? (mCamera = Camera.open();)
See the docs for a checklist as it pertains to taking pictures
Add the below three lines in manifeast file::
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
To take pictures with this class, use the following steps:
1) Obtain an instance of Camera from open(int).
2) Get existing (default) settings with getParameters().
3) If necessary, modify the returned Camera.Parameters object and call
setParameters(Camera.Parameters).
4) If desired, call setDisplayOrientation(int).
5) Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
6) Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
7) When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data.
8) After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
9) Call stopPreview() to stop updating the preview surface.
10) Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).
There are many solutions, but this is an easy, dead cheap option that worked for me:
try{
mCamera.autoFocus(autoFocusCB); //Or whatever part of code that crashes
}
catch(Exception e){
Log.v("joshtag","THIS PHONE DOES NOT SUPPORT AUTOFOCUS!!"); //a warning, popup, whatever
}
VoilĂ ! Trap deactivated.
Of course, dont forget the manifest permissions....
Related
My application turn on/off flash on a lot of devices. But it's not working on LG-F500S Android verion 6.0 (Samsung S7 android 6.0 work fine). Who help me to find reason and solution, please?
I have to find reason.
+ targetSdkVersion < 23
+ Surface Holder:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
e.printStackTrace();
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}
+ MainActivity:
public Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
#Override
protected void onResume() {
super.onResume();
cam = getCameraInstance();
mPreview = new CameraPreview(this, cam);
FrameLayout preview = (FrameLayout) findViewById(R.id.surfaceContain);
preview.addView(mPreview);
}
public void toggleLight(View v){
if (isFlash){
isFlash = false;
params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.startPreview();
}else{
isFlash = true;
params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(params);
cam.startPreview();
}
}
View details here:
https://developer.android.com/guide/topics/media/camera.html
I'm trying to capture an image from a SurfaceView custom camera. I have set up the initial code but I don't know how to call the 'take photo' method so that I can see the preview on the surface.
Currently this code displays the preview in realtime. I need to take the picture and access the byte[] array
Please help me complete the code:
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
captureImage = (ImageButton) findViewById(R.id.captureImage);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
captureImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//this button press should take the picture
}
});
mSurfaceView.getHolder().addCallback(this);
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
}
#Override
public void onPause() {
super.onPause();
mCamera.stopPreview();
}
#Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
mCamera.setParameters(params);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
I need to get the bytes [] data so that I can convert the image to a base64 string. How do I call the necessary methods to take the picture using the code above?
Check "jpegCallback" in my example:
public class CameraActivity extends ActionBarActivity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
PictureCallback jpegCallback;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
surfaceHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Toast.makeText(getApplicationContext(), "Picture Saved", 2000).show();
refreshCamera();
}
};
}
public void captureImage(View v) throws IOException {
//take the picture
camera.takePicture(null, null, jpegCallback);
}
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
// open the camera
camera = Camera.open();
} catch (RuntimeException e) {
// check for exceptions
System.err.println(e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
// modify parameter
List<Camera.Size> sizes = param.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
param.setPreviewSize(selected.width,selected.height);
camera.setParameters(param);
try {
// The Surface has been created, now tell the camera where to draw
// the preview.
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
// check for exceptions
System.err.println(e);
return;
}
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
refreshCamera();
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
// stop preview and release camera
camera.stopPreview();
camera.release();
camera = null;
}
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}
}
}
I am trying to make a camera application on Android, no errors in Log-cat but the application doesn't close when i press the back button ; and if i want to open another application camera after stopping it; the camera is locked !! any hint or ideas ?
Thanks in advance.
public class Capture extends Activity {
private Camera mCamera;
private ViewCamera mPreview;
public static final int MEDIA_TYPE_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
releaseCamera();
// Create an instance of Camera
mCamera = getCameraInstance();
Log.d("EyeSeeJD", "getCameraInstance");
// Create our Preview view and set it as the content of our activity.
mPreview = new ViewCamera(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.frame);
Log.d("EyeSeeJD", "addview ");
preview.addView(mPreview);
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
// c.lock();
} catch (Exception e) {
// Camera is not available (in use or does not exist)
Log.d("EyeSeeJD", "can't open camera ");
}
return c; // returns null if camera is unavailable
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d("EyeSeeJD",
"Error creating media file, check storage permissions ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("EyeSeeJD", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("EyeSeeJD", "Error accessing file: " + e.getMessage());
}
}
};
#Override
protected void onResume() {
super.onResume();
/*
* if (mCamera == null) { releaseCamera(); // release the camera
* immediately on pause event System.exit(0); }
*/
}
#Override
protected void onPause() {
super.onPause();
releaseCamera(); // release the camera immediately on pause event
// System.exit(0);
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
// mCamera.unlock();
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
#Override
public void onBackPressed() { // TODO Auto-generated methodstub
super.onBackPressed();
//mCamera.setPreviewCallback(null);
//mPreview.getHolder().removeCallback(mPreview);
mCamera.stopPreview();
mCamera.release(); // release the camera for other applications
mCamera = null;
finish();
System.exit(0);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
//mCamera.setPreviewCallback(null);
mCamera.takePicture(null, null, mPicture);
//mCamera.release();
// mCamera.startPreview();
return super.onTouchEvent(event);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release(); // release the camera for other applications
mCamera = null;
finish();
System.exit(0);
}
}
ViewCamera.java
public class ViewCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder = null;
private Camera mCamera = null;
public ViewCamera(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("EyeSeeJD", "Error setting camera preview: " + e.getMessage());
}
}
// check if device has camera
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
Log.d("EyeSeeJd", "there is a camera ");
return true;
} else {
// no camera on this device
Log.d("EyeSeeJd", "there is no camera ");
return false;
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// TODO Auto-generated method stub
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
/*Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(320, 480);
mCamera.setParameters(parameters);*/
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
// mCamera.unlock();
} catch (Exception e) {
Log.d("EyeSeeJd",
"Error starting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
// empty. Take care of releasing the Camera preview in your activity.
this.getHolder().removeCallback(this);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
It's hard to tell what really happens without following your code in debugger, but there is a chance that you call Camera.release() too many times. There may be NullPointerExeception somewhere that prevents normal shutdown. There should be only one release in your app, and onPause() is a perfect place to call releaseCamera(). I don't see why you need to override onBackPressed() at all, calling finish() from this override is not recommended, and calling System.exit() even more so. Also, I would move call to getCameraInstance() to onResume() or maybe to onStart().
The reason why your app takes time to close when you press back is that your app is crashing because in onBackPressed() method you are setting mCamera to null and in onDestroy() you are using it again to stop and releasing camera.
I have this activity (MakePhotoActivity) class that takes photo when you open the application. I have set it to use front facing camera.
Then I have another class, which is a SMS broadcast receiver that is already working.
Now, I would like to make it such that on receiving sms, I want it to take photo using the current class I have. But how can I integrate them together?
As I have tried copying the methods (surfaceChanged, etc) to my broadcast receiver class, and on sms receive, I placed the code that is inside the onCreate (in MakePhotoActivity). And it is not working.
public class MakePhotoActivity extends Activity implements SurfaceHolder.Callback
{
//a variable to store a reference to the Image View at the main.xml file
private ImageView iv_image;
//a variable to store a reference to the Surface View at the main.xml file
private SurfaceView sv;
//a bitmap to display the captured image
private Bitmap bmp;
private int cameraId = 0;
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Image View at the main.xml file
iv_image = (ImageView) findViewById(R.id.imageView);
//get the Surface View at the main.xml file
sv = (SurfaceView) findViewById(R.id.surfaceView);
//Get a surface
sHolder = sv.getHolder();
//add the callback interface methods defined below as the Surface View callbacks
sHolder.addCallback(this);
//tells Android that this surface will have its data constantly replaced
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d("A", "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
//get camera parameters
parameters = mCamera.getParameters();
//set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();
//sets what code should be executed after the picture is taken
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//decode the data obtained by the camera into a Bitmap
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
//set the iv_image
iv_image.setImageBitmap(bmp);
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image"+System.currentTimeMillis()+".jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
};
mCamera.takePicture(null, null, mCall);
}
public void surfaceCreated(SurfaceHolder holder)
{
// The Surface has been created, acquire the camera and tell it where
// to draw the preview.
if (mCamera == null) {
cameraId = findFrontFacingCamera();
mCamera = Camera.open(cameraId);
try {
mCamera.setPreviewDisplay(holder);
// TODO test how much setPreviewCallbackWithBuffer is faster
// mCamera.setPreviewCallback((PreviewCallback) this);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mCamera!=null)
{
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
}
}
UPDATE: This is what I have done. Put into a service, but I get an error that says app passed NULL surface, Camera server died!, ICamera died, Error 100.
I reference the code from http://easyandroidtutorials.blogspot.in/2012/09/capture-image-without-preview-as.html and made some minor changes, still can't work.
public class CameraService extends Service
{
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
SurfaceView sv;
private int cameraId = 0;
/** Called when the activity is first created. */
#Override
public void onCreate()
{
super.onCreate();
Log.i("Service", "Service started");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
if (mCamera == null) {
cameraId = findFrontFacingCamera();
mCamera = Camera.open(cameraId);
try {
// Thread.sleep(3000);
sv = new SurfaceView(getApplicationContext());
mCamera.setPreviewDisplay(sv.getHolder());
parameters = mCamera.getParameters();
//set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
} catch (IOException e) {
// TODO Auto-generated catch block
mCamera.release();
mCamera = null;
e.printStackTrace();
}
//Get a surface
//sHolder = sv.getHolder();
//tells Android that this surface will have its data constantly replaced
// sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
} catch (Exception e) {
e.printStackTrace();
}
return Service.START_STICKY;
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d("A", "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//decode the data obtained by the camera into a Bitmap
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/Image.jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
Anyone with any help on this? Thanks.
in the onReceive method of your sms broadcast reciever do this:
Intent intent = new Intent(this, MakePhotoActivity.class);
startActivity(intent);
check http://developer.android.com/training/basics/firstapp/starting-activity.html for more info on starting an activities
to take the picture in the service would require you to create a dummy surfaceview. Here's a link that should explain how to do it:
how to take camera capture without a preview from a service or thread?
if you want to disable the shutter sound:
camera.enableShutterSound(false);
http://developer.android.com/reference/android/hardware/Camera.html#enableShutterSound(boolean)
I do not have much experience with the Camera functionality in Android. I need to take a photo through code which I am planning to send back to a server. I based my logic off of this post and it works well as long as I do not add mCamera.takePicture(null, null, mPictureCallback); to the end of the surfaceChange method (which is what I want I guess). When I do add that line, it works sometimes but most of the times, I just get back a black screen. This is the main problem. I don't think there is a compatibility issue with the device because I got this functionality working atleast 3-4 times earlier.
My device that I am testing on is Galaxy Nexus
public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener
{
private static final String TAG = "CameraTest";
Camera mCamera;
boolean mPreviewRunning = false;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.e(TAG, "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera_view);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mSurfaceView.setOnClickListener(this);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
if (data != null)
{
//Intent mIntent = new Intent();
//mIntent.putExtra("image",imageData);
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
try
{
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts);
bitmap = Bitmap.createScaledBitmap(bitmap, 480, 480, false);
CameraProjectActivity.image.setImageBitmap(bitmap);
}
catch(Exception e)
{
e.printStackTrace();
}
//StoreByteImage(mContext, imageData, 50,"ImageName");
//setResult(FOTO_MODE, mIntent);
setResult(585);
finish();
}
}
};
protected void onResume()
{
Log.e(TAG, "onResume");
super.onResume();
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
protected void onStop()
{
Log.e(TAG, "onStop");
super.onStop();
}
public void surfaceCreated(SurfaceHolder holder)
{
Log.e(TAG, "surfaceCreated");
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
// XXX stopPreview() will crash if preview is not running
if (mPreviewRunning)
{
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
Camera.Size previewSize = previewSizes.get(3);
p.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(p);
try
{
mCamera.setPreviewDisplay(holder);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
// THIS IS THE CODE THAT BREAKS IT. IS THERE ANY OTHER WAY TO DO THIS??? ********
// mCamera.takePicture(null, null, mPictureCallback);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
// mCamera.stopPreview();
// mPreviewRunning = false;
// mCamera.release();
}
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
Any help would be appreciated.
Thanks!
I am not sure if this is a good solution, but I just fixed it by making the surfaceChanged() method synchronized and making the thread wait for a second just before I call takePicture.
try {
this.wait(1000);
mCamera.takePicture(null, null, mPictureCallback);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I believe that it is recommended to call startPreview() before setPreviewDisplay(), but this should not make a major difference.
What you see is that it takes time for the camera to initialize, and it cannot take a picture until it's ready. It's better to use a callback from camera to decide that it's ready, than to wait for arbitrary 1000 ms.
The callback you can use is a Camera.PreviewCallback. You can implement it in your CameraView class. To trigger the callback, simply add setOneShotPreviewCallback(this) to CameraView.surfaceChanged():
mCamera.startPreview();
mCamera.setPreviewDisplay(holder);
mCamera.setOneShotPreviewCallback(this);