I have written this code (given below) to capture photos on button click. The problem is that when I capture photos (click on button) there is no photo available/saved in specified directory (pictures folder). I am not getting any error or exception. Please tell what is issue in my code?
On Click Code:
@Override
public void onClick(View v) {
PhotoCapture.takeSnapShots(this, Camera.CameraInfo.CAMERA_FACING_BACK);
PhotoCapture.takeSnapShots(this, Camera.CameraInfo.CAMERA_FACING_FRONT);
}
Photo Capturing and Saving Code:
package com.example.appdeveloper.appname;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Environment;
import android.view.SurfaceView;
import java.io.File;
import java.io.FileOutputStream;
public class PhotoCapture {
static Camera camera = null;
static int pic_number = 0;
public static void takeSnapShots(Context context, int face) {
SurfaceView surface = new SurfaceView(context);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == face) {
try {
camera = Camera.open(camIdx);
camera.setPreviewDisplay(surface.getHolder());
camera.startPreview();
camera.takePicture(null, null, jpegCallback);
camera.stopPreview();
camera.release();
camera = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
break;
}
}
}
}
private static Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator+"picure"+(++pic_number)+".jpg";
File pictureFile = new File(path);
try {
FileOutputStream outputStream = new FileOutputStream(pictureFile);
Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix mtx = new Matrix();
mtx.setRotate(90);
realImage = Bitmap.createBitmap(realImage, 0, 0, realImage.getWidth(), realImage.getHeight(), mtx, true);
realImage.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
camera.stopPreview();
camera.release();
camera = null;
}
}
};
}