Tuesday, January 29, 2013

Android Image Capture application

In Android we can take picture and we can store it at particular file path.

make a project and have a main activity class with the name  PictureDemo.java with following code



package com.commonsware.android.picture;

import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class PictureDemo extends Activity {
private SurfaceView preview = null;
private SurfaceHolder previewHolder = null;
private Camera camera = null;
private boolean inPreview = false;
private boolean cameraConfigured = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

preview = (SurfaceView) findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void onResume() {
super.onResume();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
Camera.CameraInfo info = new Camera.CameraInfo();

for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, info);

if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
camera = Camera.open(i);
}
}
}

if (camera == null) {
camera = Camera.open();
}

startPreview();
}

@Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}

camera.release();
camera = null;
inPreview = false;

super.onPause();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.options, menu);

return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.camera) {
if (inPreview) {
camera.takePicture(null, null, photoCallback);
inPreview = false;
}
}

return (super.onOptionsItemSelected(item));
}

private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result = null;

for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;

if (newArea > resultArea) {
result = size;
}
}
}
}

return (result);
}

private Camera.Size getSmallestPictureSize(Camera.Parameters parameters) {
Camera.Size result = null;

for (Camera.Size size : parameters.getSupportedPictureSizes()) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;

if (newArea < resultArea) {
result = size;
}
}
}

return (result);
}

private void initPreview(int width, int height) {
if (camera != null && previewHolder.getSurface() != null) {
try {
camera.setPreviewDisplay(previewHolder);
} catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast.makeText(PictureDemo.this, t.getMessage(),
Toast.LENGTH_LONG).show();
}

if (!cameraConfigured) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);
Camera.Size pictureSize = getSmallestPictureSize(parameters);

if (size != null && pictureSize != null) {
parameters.setPreviewSize(size.width, size.height);
parameters.setPictureSize(pictureSize.width,
pictureSize.height);
parameters.setPictureFormat(ImageFormat.JPEG);
camera.setParameters(parameters);
cameraConfigured = true;
}
}
}
}

private void startPreview() {
if (cameraConfigured && camera != null) {
camera.startPreview();
inPreview = true;
}
}

SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
// no-op -- wait until surfaceChanged()
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
initPreview(width, height);
startPreview();
}

public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};

Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SavePhotoTask().execute(data);
camera.startPreview();
inPreview = true;
}
};

class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File folder = new File(Environment.getExternalStorageDirectory()
.toString() + "/jitesh");
folder.mkdirs();
if (folder.exists()) {
folder.delete();
}
File photo = new File(Environment.getExternalStorageDirectory()
+ "/jitesh", "photo.jpg");

if (photo.exists()) {
photo.delete();
}

try {
FileOutputStream fos = new FileOutputStream(photo.getPath());

fos.write(jpeg[0]);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}

return (null);
}
}

}

in the second step have a main.xml layout as follows


<?xml version="1.0" encoding="utf-8"?>
<android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</android.view.SurfaceView>

also this important to have options.xml inside menu  with following

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/camera"
        android:icon="@drawable/ic_menu_camera"
        
        android:title="Take Picture">
    </item>

</menu>

the following are the used resources

the given code also can be doenloaded at Android taking Picture from camera

No comments:

Post a Comment