解决闪光灯代码在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

 

posted @ 2015-09-23 17:37  HaiyuKing  阅读(401)  评论(0编辑  收藏  举报