package com.example.android.mycamera;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MyCamera extends Activity {
private static final String TAG = "MyCamera";
final int latchedOrientation = 90;
private Camera mCamera;
// Create surface for preview
private SurfaceHolder.Callback mSurfaceListener =
new SurfaceHolder.Callback() {
// Surface create
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
Log.i(TAG, "Camera opened");
try {
mCamera.setPreviewDisplay(holder);
}
catch (Exception e) {
e.printStackTrace();
}
}
// Surface destroy
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
mCamera = null;
Log.i(TAG, "Camera released");
}
// Surface change
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
//parameters.setPreviewSize(width, height);
//parameters.setPictureFormat(PixelFormat.JPEG);
parameters.set("jpeg-quality", 85);
//parameters.setRotation(latchedOrientation);
mCamera.setParameters(parameters);
mCamera.startPreview();
Log.i(TAG, "Camera preview started");
Log.i(TAG, "width : " + width + " , height : " + height);
}
};
// Auto focus to take a photo
private Camera.AutoFocusCallback mAutoFocusListener =
new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, final Camera camera) {
Log.i(TAG,"AutoFocus : " + success);
camera.autoFocus(null);
camera.takePicture(mShutterListener, null, mPictureListener);
new Thread() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
camera.startPreview();
}
}.start();
}
};
// Listener for Shut process
private Camera.ShutterCallback mShutterListener =
new Camera.ShutterCallback() {
public void onShutter() {
Log.i(TAG, "onShutter");
}
};
// Listener for Picture process
private Camera.PictureCallback mPictureListener =
new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Picture taken");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("/sdcard/MyCamera.jpg");
fos.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
// Press Camera photo button
private View.OnClickListener mButtonListener =
new View.OnClickListener() {
public void onClick(View v) {
//if(mCamera != null && mInProgress == false){
if(mCamera != null){
mCamera.autoFocus(mAutoFocusListener);
}
}
};
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Window win = getWindow();
//No Status Bar
win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//No Title Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// Camera preview
SurfaceView surface = (SurfaceView)findViewById(R.id.SurfaceView01);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(mSurfaceListener);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Camera photo button
ImageButton button = (ImageButton)findViewById(R.id.ImageButton01);
button.setOnClickListener(mButtonListener);
}
}