Android Camera Preview

I have a problem. I'm trying to develop a program that uses a camera, everything works on my device, but, as many of you know, CameraPreview does not work the same on 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 that I encountered is that I do not know how to integrate into my project, here is my code and what I did 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, which I did not copy to save space.

The question is that when I run the code, 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 FORCE CLOSE

Essentially, it does not seem to get inside OnMeasure to fix 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) 
+4
source share
1 answer

can you hand over the magazine? I predict that you have a problem with mPreview.width / height - this is possibly an incorrect value.

0
source

Source: https://habr.com/ru/post/1339974/


All Articles