Camera Preview: No errors but no picture :-(
Have read the books and hints, got rid of all compile errors and warnings and put in some debug statements.
package com.cit.BroadcastSim;
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;
public class BroadcastActivity extends Activity implements SurfaceHolder.Callback {
public Camera myCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.broadcast); // Inflate the broadcast.xml file
Log.d("BROADCAST", "Creating the Activity");
SurfaceView cameraSurface = (SurfaceView)findViewById(R.id.camerasurface);
SurfaceHolder cameraHolder = cameraSurface.getHolder();
cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraHolder.addCallback(this);
Log.d("BROADCAST", "Now wait for some CallBacks");
}
public void surfaceCreated(SurfaceHolder holder) {
// Surface created, now it is possible to set the preview
Log.d("BROADCAST", "Surface Created");
try {
Log.d("BROADCAST","CAMERA: NOT NULL");
myCamera = Camera.open();
myCamera.setPreviewDisplay(holder);
myCamera.开发者_开发技巧startPreview();
} catch (IOException e) {
Log.d("BROADCAST", e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("BROADCAST", "Surface Destroyed");
myCamera.stopPreview();
myCamera.release();
}
public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
Log.d("BROADCAST", "Surface Changed");
myCamera.stopPreview();
myCamera.release();
}
}
In the DDMS debugger, I get a log message for 'Creating the Activity' followed by 'Now wait for some CallBacks' and nothing more in terms of my Debug messages so I think Callback is not working - for the life of me can't see where I have got it wrong.
In the manifest I have
The Activity XML page has
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Broadcast"
/>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/camerasurface"
/>
</LinearLayout>
Finally, on the Android phone (HTC Wildfire), the page loads, the textView message appears at the top left and that is all.
Should mention that I am very new to this platform and accept that I might have missed something very very basic.
Any ideas/comments will be very much appreciated,
Oliver
take a look at the ApiDemos (Graphics->CameraPreview) and the class CameraPreview. That was what I did (all the ApiDemos are great btw) and it worked like a charm. It's good to have something working first - you can then strip it off the stuff you don't need in your app. The demo is also online here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html. Hope that helped.
haven't got enough time to explain (meeting with friends in a few minutes). I basically added some functions into "surfaceChanged()" (this is where you should start the preview).
Therefore you don't need mCam.startPreview() in "surfaceCreated()"
public void surfaceCreated(SurfaceHolder holder) {
// Surface created, now it is possible to set the preview
Log.d("BROADCAST", "Surface Created");
try {
Log.d("BROADCAST","CAMERA: NOT NULL");
mCam = Camera.open();
mCam.setPreviewDisplay(holder);
//myCamera.startPreview();
} catch (IOException e) {
Log.d("BROADCAST", e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("BROADCAST", "Surface Destroyed");
mCam.stopPreview();
mCam.release();
}
public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
Camera.Parameters parameters = mCam.getParameters();
List<Size> previewSizes = parameters.getSupportedPreviewSizes();
Size bestSize = null;
int bestDiff = 0;
int diff = 0;
for (Size size : previewSizes) {
diff = Math.abs(K - size.height) + Math.abs(J - size.width);
if (bestSize == null || diff < bestDiff) {
bestSize = size;
bestDiff = diff;
}
parameters.setPreviewSize(bestSize.width, bestSize.height);
mCam.setParameters(parameters);
}
//start preview of camera
mCam.startPreview();
}
xml-file (you did a little copy & paste error)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
Hope I could help :) I think, via setting the orientation, you could change the camera picture to portrait mode. :) (it's landscape right now)
To be honest I think that whole camera preview handling "by hand" is overly complex. If you just want to take a picture and have a preview first, you can tell this the system via an Intent. I've written a post about this some time ago http://javablogs.com/Jump.action?id=618025 Basically it goes:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
And then within your activity you can get the data in a onActivityResult()
callback
@Override
public void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&& resultCode==RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
}
}
If you need a larger image than the default, have a look at MediaStore.EXTRA_OUTPUT.
精彩评论