运行低于Lollipop的设备上的CameraManager

时间:2015-06-09 17:05:14

标签: java android android-5.0-lollipop

我遇到的问题是我同时使用Camera(低于Lollipop设备)和CameraManager类(Lollipop及以上设备)。

运行低于Lollipop的设备崩溃,我相信这是因为

import android.hardware.camera2.CameraManager;

一部分。 我该怎么做呢

package com.example.DeviceCommands;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;

public class FlashController
{
    Context _context;
    boolean _is_supported = false;
    boolean _enabled = false;
    Object _camera;

    @TargetApi(Build.VERSION_CODES.L) public FlashController(Context context)
    {
        if (context.getPackageManager().hasSystemFeature(PackageManager
            .FEATURE_CAMERA_FLASH))
        {
            _context = context;
            _is_supported = true;

            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
            {
                Camera camera = Camera.open();

                if (camera == null)
                {
                    _is_supported = false;
                    System.out.println("NOT SUPPORTED!");
                    return;
                }

                _camera = camera;
            }
            else
            {
                CameraManager camera = (CameraManager) _context.getSystemService(Context.CAMERA_SERVICE);
                _camera = camera;
            }
        }
    }


    @TargetApi(Build.VERSION_CODES.L) public void enableFlashLight()
    {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
        {
            Camera camera = (Camera) _camera;
            Camera.Parameters parameters = camera.getParameters();

            if(!parameters.getFlashMode().equals(android.hardware
                                        .Camera.Parameters.FLASH_MODE_TORCH)
                                        && !_enabled)
            {  
                Parameters params = camera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(params);
                camera.startPreview();
                _enabled = true;
                camera.autoFocus(new AutoFocusCallback()
                {
                    public void onAutoFocus(boolean success, Camera camera)
                    {

                    }
                });
            }
        }
        else
        {
            //CameraManager manager =(CameraManager) _context.getSystemService(Context.CAMERA_SERVICE);

            try
            {
                CameraManager camera = (CameraManager) _camera;

                for(int i = 0; i < camera.getCameraIdList().length; i++)
                {
                    System.out.println("Camera=" + camera.getCameraCharacteristics(
                                                   camera.getCameraIdList()[i]));
                }
            }
            catch (CameraAccessException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void disableFlashLight()
    {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
        {
            Camera cam = Camera.open();
            Camera.Parameters parameters = cam.getParameters();

            if(parameters.getFlashMode().equals(android.hardware
                                        .Camera.Parameters.FLASH_MODE_TORCH))
            {
                parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(parameters);
                cam.release();
            }

        }
    }   

}
如果import部分会使设备运行低于Lollipop崩溃,

工作吗?

感谢您提供任何帮助和建议!

编辑: 添加了上面的全班。 Logcat输出

06-09 19:44:30.219: E/ViewRootImpl(956): sendUserActionEvent() mView == null
06-09 19:44:31.360: E/dalvikvm(956): Could not find class 'android.hardware.camera2.CameraManager', referenced from method com.example.DeviceCommands.FlashController.<init>
06-09 19:44:31.360: W/dalvikvm(956): VFY: unable to resolve check-cast 122 (Landroid/hardware/camera2/CameraManager;) in Lcom/example/DeviceCommands/FlashController;
06-09 19:44:31.360: D/dalvikvm(956): VFY: replacing opcode 0x1f at 0x003a
06-09 19:44:31.367: E/dalvikvm(956): Could not find class 'android.hardware.camera2.CameraManager', referenced from method com.example.DeviceCommands.FlashController.enableFlashLight
06-09 19:44:31.367: W/dalvikvm(956): VFY: unable to resolve check-cast 122 (Landroid/hardware/camera2/CameraManager;) in Lcom/example/DeviceCommands/FlashController;
06-09 19:44:31.367: D/dalvikvm(956): VFY: replacing opcode 0x1f at 0x0040
06-09 19:44:31.367: W/dalvikvm(956): VFY: unable to resolve exception class 120 (Landroid/hardware/camera2/CameraAccessException;)
06-09 19:44:31.367: W/dalvikvm(956): VFY: unable to find exception handler at addr 0x6b
06-09 19:44:31.367: W/dalvikvm(956): VFY:  rejected Lcom/example/DeviceCommands/FlashController;.enableFlashLight ()V
06-09 19:44:31.367: W/dalvikvm(956): VFY:  rejecting opcode 0x0d at 0x006b
06-09 19:44:31.367: W/dalvikvm(956): VFY:  rejected Lcom/example/DeviceCommands/FlashController;.enableFlashLight ()V
06-09 19:44:31.367: W/dalvikvm(956): Verifier rejected class Lcom/example/DeviceCommands/FlashController;
06-09 19:44:31.367: D/AndroidRuntime(956): Shutting down VM
06-09 19:44:31.367: W/dalvikvm(956): threadid=1: thread exiting with uncaught exception (group=0x4100d930)
06-09 19:44:31.375: E/AndroidRuntime(956): FATAL EXCEPTION: main
06-09 19:44:31.375: E/AndroidRuntime(956): java.lang.VerifyError: com/example/DeviceCommands/FlashController
06-09 19:44:31.375: E/AndroidRuntime(956):  at com.example.babymonitorer.MainActivity.onClick(MainActivity.java:121)
06-09 19:44:31.375: E/AndroidRuntime(956):  at android.view.View.performClick(View.java:4421)
06-09 19:44:31.375: E/AndroidRuntime(956):  at android.view.View$PerformClick.run(View.java:18190)
06-09 19:44:31.375: E/AndroidRuntime(956):  at android.os.Handler.handleCallback(Handler.java:725)
06-09 19:44:31.375: E/AndroidRuntime(956):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 19:44:31.375: E/AndroidRuntime(956):  at android.os.Looper.loop(Looper.java:175)
06-09 19:44:31.375: E/AndroidRuntime(956):  at android.app.ActivityThread.main(ActivityThread.java:5279)
06-09 19:44:31.375: E/AndroidRuntime(956):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 19:44:31.375: E/AndroidRuntime(956):  at java.lang.reflect.Method.invoke(Method.java:511)
06-09 19:44:31.375: E/AndroidRuntime(956):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-09 19:44:31.375: E/AndroidRuntime(956):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-09 19:44:31.375: E/AndroidRuntime(956):  at dalvik.system.NativeStart.main(Native Method)

MainActivity第121行:FlashController fc = new FlashController(this);

2 个答案:

答案 0 :(得分:0)

我遇到过类似的问题。唯一的区别是我已将SDK版本更新为Android M(API 23)。

  1. 首先将SDK更新为API 23.

  2. 然后添加检查:Build.VERSION.SDK_INT >= Build.VERSION_CODES.M而非Lollipop版本。

  3. 然后您可以尝试以下Android M版及以上版本的代码:

  4. if (context.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
    
        CameraManager camera = (CameraManager) _context.getSystemService(Context.CAMERA_SERVICE);
    
    }
    

    希望这有帮助。

答案 1 :(得分:0)

只需将您的班级划分为两个:一个用于API2,另一个用于API1。首先检查安装了什么android版本,然后运行相应的类。 这样做:

    // Start Camera capture Image display
public void intentStartCamera() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Intent myIntent = new Intent(CaptureDetailsActivity.this, CaptureActivityCAPI2.class);
        startActivityForResult(myIntent, OPEN_CAMERA_REQUEST);
    } else {
        Intent myIntent = new Intent(CaptureDetailsActivity.this, CaptureActivityCAPI1.class);
        startActivityForResult(myIntent, OPEN_CAMERA_REQUEST);
    }
}

这是我在这个项目中的解决方案。