Reputation: 377
I need to open camera on button click, my need is when i click on button then back camera get open and after clicking on capture photo, i need to open front camera as well. Please help me out.
Picture Call back code:
PictureCallback jpegCallback = new PictureCallback() {
@SuppressWarnings("deprecation")
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
String filepath = Environment.getExternalStorageDirectory().getPath();
Calendar c = Calendar.getInstance();
File videoDirectory = new File(filepath,"Spooks");
if (!videoDirectory.exists()) {
videoDirectory.mkdirs();
}
try {
Calendar _cal = Calendar.getInstance();
long _timeStamp = _cal.getTimeInMillis();
////System.out.println( "Time stamp"+_timeStamp);
String fileToSend = videoDirectory.getAbsolutePath() + "/" +"Image_"+ _timeStamp +".png";
// Write to SD Card
System.out.println("Write to SD Card");
outStream = new FileOutputStream(fileToSend);
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Bitmap realImage;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
options.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
options.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);
ExifInterface exif = null;
try {
exif = new ExifInterface(path + c.getTime().getSeconds()
+ ".jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Log.d("EXIF value",
exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
.equalsIgnoreCase("1")) {
realImage = rotate(realImage, 90);
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
.equalsIgnoreCase("8")) {
realImage = rotate(realImage, 90);
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
.equalsIgnoreCase("3")) {
realImage = rotate(realImage, 90);
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
.equalsIgnoreCase("0")) {
realImage = rotate(realImage, 90);
}
} catch (Exception e) {
}
camera.startPreview();
// camera.stopPreview();
System.out.println("Back cam image saved");
System.out.println("surfaceDestroyed on activity");
/*_camera.stopPreview();
_camera.release();*/
// finish();
// _camera.release();
// _camera.stopPreview();
//need to open from camera
_camera.release();
_camera = null;
if(_camera==null){
_camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
_camera.startPreview();
_camera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera mcamera) {
_camera.release();
_camera = Camera.open();
Log.d("Camera died", "error camera");
}
});
}
if (_camera != null) {
if (Build.VERSION.SDK_INT >= 14)
setCameraDisplayOrientation(context,
CameraInfo.CAMERA_FACING_FRONT, _camera);
preview.setCamera(_camera);
}
}
};
Upvotes: 1
Views: 814
Reputation: 6980
In your OnPictureTakenCallback
open front camera.
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
public static final String TAG = "error";
@Override
public void onPictureTaken(byte[] mData, Camera camera) {
/*
Here do something with the taken picture, then stop
the preview, release back camera and open front camera
*/
}
}
Be careful while switching camera because you have to release current camera you are holding before taking on other.
EDIT:
open()
method takes int
argument as id of the camera. If you call open
method without passing anything, it will open back camera. To open front camera you have to pass id of the front camera to open
method.
Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
Upvotes: 1