• 解决闪光灯代码在Nexus5上面打不开的问题


    参考:http://blog.csdn.net/cy524563/article/details/41545387

    关键在于:

    int textureId = 0;
    whyCamera.setPreviewTexture(new SurfaceTexture(textureId));//关键代码:解决在Nexus5上面打不开闪光灯的问题

    之前在网上找到的关键代码均不含有以上两行代码,所以导致在Nexus5手机上面打不开闪光灯。

    详细代码如下:自定义了一个类,用于执行闪关灯的打开、关闭、销毁等功能。

    package com.why.flashlight50.model;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.FeatureInfo;
    import android.content.pm.PackageManager;
    import android.graphics.SurfaceTexture;
    import android.hardware.Camera;
    import android.hardware.Camera.AutoFocusCallback;
    import android.hardware.Camera.Parameters;
    import android.util.Log;
    import android.widget.Toast;
    
    public class LedCamera {
        
        private static final String TAG = "LedCamera";
        private static Camera whyCamera = null;
        private static Parameters whyparameters = null;
        private static Activity activityObj;//activity对象,由activity界面中传入:比如MainActivity,用于获取view
        
        //给whyCamera变量赋值
        public static void prepareCameraLed(Activity activity){
            activityObj = activity;
            if (!isSupportFlashlightSimple(activityObj)) {
                Toast.makeText(activity, "当前设备没有闪光灯", Toast.LENGTH_LONG).show();
                return;
            }else{
                if(whyCamera == null){
                    
                    try{
                        whyCamera = Camera.open();
                        Log.v("ObjectCode", "LedCamera:whyCamera="+whyCamera.hashCode());
                        Log.v("ObjectCode", "LedCamera:activityObj="+activityObj.hashCode());
                        return;
                    }catch(Exception localException){
                        localException.printStackTrace();
                    }
                    
                }
            }
            
        }
        
        //给whyCamera变量赋值
        public static void prepareCameraLed(Context context){
            if (!isSupportFlashlightSimple(context)) {
                Toast.makeText(context, "当前设备没有闪光灯", Toast.LENGTH_LONG).show();
                return;
            }else{
                if(whyCamera == null){
                    
                    try{
                        whyCamera = Camera.open();
                        Log.v("ObjectCode", "LedCamera:whyCamera="+whyCamera.hashCode());
                        Log.v("ObjectCode", "LedCamera:activityObj="+context.hashCode());
                        return;
                    }catch(Exception localException){
                        localException.printStackTrace();
                    }
                    
                }
            }
            
        }
        
        //开启闪光灯
        public static void openCameraLed(){
            
            if(whyCamera != null){
                
                try{
                    int textureId = 0;
                    whyCamera.setPreviewTexture(new SurfaceTexture(textureId));//关键代码:解决在Nexus5上面打不开闪光灯的问题
                    
                    whyparameters = whyCamera.getParameters();
                    Log.v(TAG,"openCameraLed=="+whyparameters.getFlashMode());
                    if ((whyparameters != null) && (whyparameters.getFlashMode() != null))
                    {
                        whyparameters.setFlashMode("torch");//修改为字符串
                        whyCamera.setParameters(whyparameters);
                    }
                    
                    Log.v(TAG,"openCameraLed=="+whyparameters.getFlashMode());
                    //打开预览
                    whyCamera.startPreview();
                    
                    return;
                    
                }catch(Exception localException){
                    localException.printStackTrace();
                }
            }
        }
        
        //关闭闪光灯
        public static void closeCameraLed(){
            
            if(whyCamera != null){
                
                try{
                    
                    whyparameters = whyCamera.getParameters();
                    Log.v(TAG,"closeCameraLed=="+whyparameters.getFlashMode());
                    if ((whyparameters != null) && (whyparameters.getFlashMode() != null))
                    {
                        whyparameters.setFlashMode("off");//修改为字符串
                        whyCamera.setParameters(whyparameters);
                    }
                    Log.v(TAG,"closeCameraLed=="+whyparameters.getFlashMode());
                    //关闭预览
                    whyCamera.stopPreview();
                    return;
                    
                }catch(Exception localException){
                    localException.printStackTrace();
                }
            }
    
        }
        
        //释放camera资源
        public static void releaseCameraLed()
        {
            if (whyCamera != null){
                try
                {
                    whyCamera.release();
                    whyCamera = null;
                    return;
                }
                catch (Exception localException)
                {
                    localException.printStackTrace();
                }
            }
        }
    
        
        /** 
         * 判断手机是否支持闪光灯 
         * @param context 
         * @return 
         */  
        public static boolean isSupportFlashlight(Context context) {  
            PackageManager packageManager = context.getPackageManager();  
            FeatureInfo[]  features = packageManager.getSystemAvailableFeatures();  
            for(FeatureInfo f : features) {  
                if(PackageManager.FEATURE_CAMERA_FLASH.equals(f.name))   
                    return true;  
            }
            return false;  
        }
        /** 
         * 判断手机是否支持闪光灯--简单版
         * @param context 
         * @return 
         */  
        public static boolean isSupportFlashlightSimple(Context context) {  
            PackageManager packageManager = context.getPackageManager();
            if(packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
                return true;  
            }
            return false;
        }
        
        
    }
    LedCamera
  • 相关阅读:
    Matlab 整数线性规划问题模型代码
    Matlab 非线性规划问题模型代码
    Matlab 线性规划问题模型代码
    多波次导弹发射中的规划问题(二) 问题一解答
    多波次导弹发射中的规划问题(一) 网络图绘制及数据整理
    多无人机对组网雷达的协同干扰问题 数学建模
    余胜威《MATLAB数学建模经典案例实战》2015年版
    袁新生《LINGO和Excel在数学建模中的应用》
    卓金武《MATLAB在数学建模中的应用》 第2版
    司守奎《数学建模算法与应用》 第二版
  • 原文地址:https://www.cnblogs.com/whycxb/p/4832926.html
Copyright © 2020-2023  润新知