安卓相机预览
我有一个问题。 我正在尝试开发一个使用相机的程序,一切都在我的设备中正常工作,但就像你们中的许多人都知道 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();
}
为空,并且我得到强制关闭,
本质上看起来它没有进入 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)
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:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能传递日志吗?
我预测您的 mPreview.width/height 有问题 - 它可能是一些不正确的值。
can you pass the log?
I predict that you have a problem with mPreview.width/height - it is propably some incorrect value.