问题
I have a problem.
I'm trying to develop a program that uses the camera, everything is working in my device but like many of you know the CameraPreview
doesn't work the same way in all devices, so I tried to integrate the code that google provides in this address:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
The problem I've faced is that I don't know how to integrate to my project, here is my code and what I've done without exceeding:
Preview mPreview;
private Camera mCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreview = new Preview(this);
//ContentView----------------------------------------------------------
setContentView(R.layout.main);
mSurfaceView = (SurfaceView)findViewById(R.id.camera_surface);
mPreview.surfacemetod(mSurfaceView);
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mCamera = Camera.open();
mPreview.setCamera(mCamera);
Then the Preview class:
class Preview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Preview";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;
Preview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
}
public void surfacemetod(SurfaceView surface){
//mSurfaceView = new SurfaceView(context);
//addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void setCamera(Camera camera) {
mCamera = camera;
if (mCamera != null) {
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
requestLayout();
}
}
After this code there are all the methods in the link above that I didn't copy to save space.
The question is that when I run the code the mPreviewSize.with and mPreviewSize.height
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
are null and I get a FORCE CLOSE
Essentially looks like it doesn't get inside OnMeasure to resolve them. Any idea?
This is the log:
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): FATAL EXCEPTION: main
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): java.lang.NullPointerException
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at com.dashboard.camera.lite.Preview.surfaceChanged(Preview.java:162)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:341)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.View.draw(View.java:6796)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.widget.FrameLayout.draw(FrameLayout.java:354)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.View.draw(View.java:6796)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.widget.FrameLayout.draw(FrameLayout.java:354)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1894)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewRoot.draw(ViewRoot.java:1432)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewRoot.performTraversals(ViewRoot.java:1174)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.os.Looper.loop(Looper.java:123)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-16 13:54:26.068: ERROR/AndroidRuntime(24374): at dalvik.system.NativeStart.main(Native Method)
回答1:
can you pass the log? I predict that you have a problem with mPreview.width/height - it is propably some incorrect value.
来源:https://stackoverflow.com/questions/5015635/android-camera-preview