I've developing a lock screen while users wrong attempt the camera should automatically start and take picture but i didn't find any solution or API
This code first declares the variables that will act as handles to the Surface View and the Image View at the activity_main.xml file. Then, a Bitmap object is declared, and it will be used to display the image after it has been captured (line 23). After that, 3 objects are declared: a SurfaceHolder, that will allocate a part of the screen to render the camera preview (which has 0 width and height); a Camera that will handle the device’s camera; and a Parameters object, that will be used to set the camera’s settings (lines 27 through 31).
Moving on to the onCreate() method, it basically initializes all the declared objects by getting a reference to other existing ones, like the sv object that will make a reference to the SurfaceView in the main.xml file. There are two lines inside this method that need a more detailed explanation. Line 50 sets the Surface Holder callback to this, because this class is implementing the SurfaceHolder.Callback interface, that has the purpose of controlling the rendering of a “surface” (a area of the screen). This is required so that the “preview” works. The other important line is the 53rd one, that tells Android that this surface will be having all its data being replaced.
The SurfaceChanged() method is where it all happens. The parameters object is initialized (line 60). Not only that, the camera parameters are set, and the preview is started (lines 63 and 64). The picture callback is defined, it’s code being called every time a picture is taken (lines 67 through 77). Inside it, the data captured by the camera is decoded into the Bitmap object (line 73) and right after that, line 75 tells the ImageView to display this Bitmap. At the end of the method, the camera is requested to take a picture, using the recently created callback (line 79).
The code inside the surfaceCreated() method hooks the camera object to the device’s camera. It also tells where the camera should preview its capture (lines 83 through 95). The last one, method surfaceDestroyed() releases the camera, so it can be used by other applications (lines 98 through 106).
It’s a pretty standard camera capturing code. What hides the preview is the width and height of the Surface Holder, that are set to zero. The last requirement needed to make it work is to add a permission to access the camera to the AndroidManifest file:
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
MainActivity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=(ImageView)findViewById(R.id.imageView1);
// do we have a camera?
if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
.show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
safeCameraOpen(cameraId);
}
}
// THIS IS JUST A FAKE SURFACE TO TRICK THE CAMERA PREVIEW
// http://stackoverflow.com/questions/17859777/how-to-take-pictures-in-android-
// application-without-the-user-interface
SurfaceView view = new SurfaceView(this);
try {
camera.setPreviewDisplay(view.getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
Camera.Parameters params = camera.getParameters();
params.setJpegQuality(100);
camera.setParameters(params);
// We need something to trigger periodically the capture of a
// picture to be processed
timer=new Timer(getApplicationContext(),threadHandler);
timer.execute();
}
////////////////////////////////////thread Handler///////////////////////////////////////
private Handler threadHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case DONE:
// Trigger camera callback to take pic
camera.takePicture(null, null, mCall);
break;
case NEXT:
timer=new Timer(getApplicationContext(),threadHandler);
timer.execute();
break;
}
}
};
Camera.PictureCallback mCall = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//decode the data obtained by the camera into a Bitmap
//display.setImageBitmap(photo);
Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(data, 0, data.length);
display.setImageBitmap(bitmapPicture);
Message.obtain(threadHandler, MainActivity.NEXT, "").sendToTarget();
//Log.v("MyActivity","Length: "+data.length);
}
};
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.v("MyActivity", "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
@Override
protected void onPause() {
if (timer!=null){
timer.cancel(true);
}
releaseCamera();
super.onPause();
}
// I think Android Documentation recommends doing this in a separate
// task to avoid blocking main UI
private boolean safeCameraOpen(int id) {
boolean qOpened = false;
try {
releaseCamera();
camera = Camera.open(id);
qOpened = (camera != null);
} catch (Exception e) {
Log.e(getString(R.string.app_name), "failed to open Camera");
e.printStackTrace();
}
return qOpened;
}
private void releaseCamera() {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
}
Exception
02-02 12:44:58.941: E/AndroidRuntime(2067): FATAL EXCEPTION: main
02-02 12:44:58.941: E/AndroidRuntime(2067): java.lang.RuntimeException: takePicture failed
02-02 12:44:58.941: E/AndroidRuntime(2067): at android.hardware.Camera.native_takePicture(Native Method)
02-02 12:44:58.941: E/AndroidRuntime(2067): at android.hardware.Camera.takePicture(Camera.java:1146)
02-02 12:44:58.941: E/AndroidRuntime(2067): at android.hardware.Camera.takePicture(Camera.java:1091)
02-02 12:44:58.941: E/AndroidRuntime(2067): at com.example.capturefindmyphone.MainActivity$1.handleMessage(MainActivity.java:80)
02-02 12:44:58.941: E/AndroidRuntime(2067): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 12:44:58.941: E/AndroidRuntime(2067): at android.os.Looper.loop(Looper.java:137)
02-02 12:44:58.941: E/AndroidRuntime(2067): at android.app.ActivityThread.main(ActivityThread.java:4960)
02-02 12:44:58.941: E/AndroidRuntime(2067): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 12:44:58.941: E/AndroidRuntime(2067): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 12:44:58.941: E/AndroidRuntime(2067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-02 12:44:58.941: E/AndroidRuntime(2067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-02 12:44:58.941: E/AndroidRuntime(2067): at dalvik.system.NativeStart.main(Native Method)