我有问题。
我正在尝试开发一个使用相机的程序,一切都在我的设备中工作但是很多人都知道CameraPreview
在所有设备中的工作方式都不一样,所以我尝试集成代码谷歌在这个地址提供:
我遇到的问题是我不知道如何整合到我的项目中,这是我的代码以及我所做的事情,但不超过:
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);
然后预览类:
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();
}
}
在此代码之后,上面链接中的所有方法都没有复制以节省空间。
问题是,当我运行代码时,mPreviewSize.with和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();
}
为空,我得到一个FORCE CLOSE
基本上看起来它没有进入OnMeasure来解决它们。 有什么想法吗?
这是日志:
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)
答案 0 :(得分:0)