views:

364

answers:

1

All, I've googled over and over again to find a solution and while I found a bug regarding camera release, etc I can not for the life of me seem to get the cam code to work. Every time I executed takePicture the system simply hangs, sometimes it calls the PictureCallback, but most of the time it simply hangs.

Weird issues about not being able to read /data/ap_gain.bin files, etc

Below is the code:

public class CameraActivity extends Activity implements Camera.PictureCallback, RequestConstants {

private static final String TAG = "Camera";

private Preview preview;

private boolean previewRunning;

private int addressNotificationId;

@Override protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

addressNotificationId = getIntent().getIntExtra(REQ_RA_ID, 0);

getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

if (preview == null) { preview = new Preview(this); } setContentView(preview); }

@Override protected void onDestroy() { if (isFinishing()) { preview.cleanup(); }

super.onDestroy(); }

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

/* preview.setDrawingCacheEnabled(true); Bitmap ss = preview.getDrawingCache(); byte[] data = ImageUtility.getImageData(ss,75,1); Log.v(TAG, "Pic with size: " + data.length);

ApplicationManager.getInstance().createPacketRecord(PacketConstants.PT_FLAG_ADDRESS_PHOTO, ApplicationDatabaseManager.getInstance().getRouteAddressBySystemId(addressNotificationId), data); finish(); */ preview.getCamera().takePicture(new Camera.ShutterCallback() { @Override public void onShutter() {

}

}, null, this); return true; } return super.onKeyDown(keyCode, event); }

@Override public void onPictureTaken(byte[] data, Camera camera) { /* if (data == null || isFinishing()) return;

camera.stopPreview(); previewRunning = false; camera.release(); */ //Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length); //data = null; //data = ImageUtility.getImageData(bitmap, 75,1);

Log.v(TAG, "Pic with size: " + data.length);

ApplicationManager.getInstance().createPacketRecord(PacketConstants.PT_FLAG_ADDRESS_PHOTO, ApplicationDatabaseManager.getInstance().getRouteAddressBySystemId(addressNotificationId), data); finish(); }

}

class Preview extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; Camera mCamera;

Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();

    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

Camera getCamera() {
 return mCamera;
}

void cleanup() {
 mCamera.stopPreview();
    mCamera.release();
    mCamera = null;
}

public void surfaceCreated(SurfaceHolder holder) {
    if (mCamera == null)
     mCamera = Camera.open();

    try {
       mCamera.setPreviewDisplay(holder);
    } catch (IOException exception) {
        mCamera.release();
        mCamera = null;
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {

}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(w, h);
    parameters.setPictureSize(w, h);
    mCamera.setParameters(parameters);
    mCamera.startPreview();
}

}

A: 

After you JpegPictureCallback is called, you need to call mCamera.startPreview again.

There isn't enough info in the API Camera sample to really write a camera app, but you can get the source code for Google's own camera app here. Camera.java contains a lot of important, useful code:

git://android.git.kernel.org/platform/packages/apps/Camera.git

Corey Trager