Android Programming Tutorials

(Romina) #1
Photographic Memory

Step #3: Create the Photographer Class


Next, we need a class that will both connect the live preview to the


SurfaceView, plus intercept the camera button for use in taking an actual


picture. To keep things simple, we will hold off for now on doing anything


"for real" with the actual picture.


With that in mind, add a Photographer class that looks like the following:


package apt.tutorial;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class Photographer extends Activity {
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;

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

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_CAMERA ||
keyCode==KeyEvent.KEYCODE_SEARCH) {
takePicture();

return(true);
}

return(super.onKeyDown(keyCode, event));
}

352
Free download pdf