简体   繁体   English

自定义相机方向问题

[英]Custom Camera Orientation Issue

I have built a custom camera application . 我建立了一个自定义相机应用程序。 Facing a problem in screen orientation and I don't want the activity to be re-created when the orientation changes. 遇到屏幕方向问题,我不希望在方向改变时重新创建活动。 Please help me on this with the complete solution. 请为我提供完整的解决方案。

Getting a NULL pointer exception inside the surfaceChanged method in the preview class. 在Preview类的surfaceChanged方法内部获取NULL指针异常。

  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        if (mCamera != null) {
            Camera.Parameters parameters = mCamera.getParameters();
            Log.d("SurfaceChanged", "getting parameters" + parameters);

Getting a NULL pointer exception in the below line : 在下面的行中获取NULL指针异常:

        Display display =                               ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        Log.d("SurfaceChanged", "Getting the system services");
        if(display.getRotation() == Surface.ROTATION_0){
            Log.d("SurfaceChanged", "Inside the if condition 1");
            parameters.setPreviewSize(height,width);
            Log.d("SurfaceChanged", "setting preview size");
            mCamera.setDisplayOrientation(90);
            Log.d("SurfaceChanged", "setting display orientation as 90");
        }

        if(display.getRotation() == Surface.ROTATION_90){
            Log.d("SurfaceChanged", "Inside the if condition 2");
            parameters.setPreviewSize(width,height);
            Log.d("SurfaceChanged", "setting the preview size 2 ");
        }

        if(display.getRotation() == Surface.ROTATION_180){
            Log.d("SurfaceChanged", "Inside the if condition 3");
            parameters.setPreviewSize(height,width);
            Log.d("SurfaceChanged", "setting the preview size 3");
        }

        if(display.getRotation() == Surface.ROTATION_270){
            Log.d("SurfaceChanged", "Inside the if condition 4");
            parameters.setPreviewSize(width,height);
            Log.d("SurfaceChanged", "setting preview size 4");
            mCamera.setDisplayOrientation(180);
            Log.d("SurfaceChanged", "setting display orientation as 180");
        }

        mCamera.setParameters(parameters);
        Log.d("SurfaceChanged", "setting the parameters");
       try {
            mCamera.setPreviewDisplay(mHolder);
            Log.d("SurfaceChanged", "setting preview display to mHolder");
            mCamera.startPreview();
            Log.d("SurfaceChanged", "preview as started");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is the error log: 这是错误日志:

12-23 16:07:34.962 8845-8845/com.clarity.camera E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.clarity.camera , PID: 8845 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at com.clarity.camera.CameraPreview.surfaceChanged(CameraPreview.java:230) at android.view.SurfaceView.updateWindow(SurfaceView.java:583) at android.view.SurfaceView.access$000(SurfaceView.java:86) at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.v 12-23 16:07:34.962 8845-8845 / com.clarity.camera E / AndroidRuntime:致命异常:主进程:com.clarity.camera,PID:8845 java.lang.NullPointerException:尝试调用虚拟方法'java。 lang.Object android.content.Context.getSystemService(java.lang.String)'在com.clarity.camera.CameraPreview.surfaceChanged(CameraPreview.java:230)上的空对象引用上,在android.view.SurfaceView.updateWindow(SurfaceView .java:583)android.view.SurfaceView.access $ 000(SurfaceView.java:86)android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:175)android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java: 847)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5725)在android.v上的android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)android.v上的android.view.Choreographer.doCallbacks(Choreographer.java:574) iew.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5086) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)位于android.os.Handler.handleCallback(Handler.java:733)的iew.Choreographer.doFrame(Choreographer.java:544) .dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:136)在android.app.ActivityThread.main(ActivityThread.java:5086)在java.lang.reflect.Method.invoke( com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)上的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)上的Native方法)

If you don't want your Activity to be destroyed you can place this in your activity tags in you manifest file : 如果您不希望破坏Activity ,可以将其放置在manifest file中的活动标签中:

android:configChanges="orientation|screenSize|keyboardHidden"

This will tell Android that you are going to handle orientationChanges by yourself. 这将告诉Android的,你要处理orientationChanges自己。 screenSize is important if you are developing on Android API 13 or higher. 如果您使用的是Android API 13或更高版本,则screenSize非常重要。 If you are developing for Android API 12 or lower you can leave screenSize out! 如果您正在开发Android API 12或更低版本,则screenSize

That' means onDestroy will not be called anymore. 这意味着onDestroy将不再被调用。 Instead onConfigurationChanged is called and you can handle what ever you want to do in this method when a Rotation of the device occurs. 而是onConfigurationChanged ,并且当设备发生旋转时,您可以使用此方法进行处理。

See here: http://developer.android.com/guide/topics/resources/runtime-changes.html 参见此处: http : //developer.android.com/guide/topics/resources/runtime-changes.html

TO help you with the NullPointerException you will Need to post your LogCat Output and tell us which line exactly throws the NPE! 为了帮助您解决NullPointerException,您需要发布LogCat输出并告诉我们哪一行恰好抛出NPE!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM