2014-01-22 86 views
6

我正在寫一個Android相機應用程序,並試圖實現Flash功能,我使用SurfaceView,每當我點擊閃光按鈕切換作品,但閃光燈沒有來...看到我的下面的代碼我已經習慣打開/關閉手電筒。打開/關閉使用SurfaceView的手電筒

完整代碼: -

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_camera); 

      preview=(SurfaceView)findViewById(R.id.surface);  
       previewHolder=preview.getHolder();  
       previewHolder.addCallback(surfaceCallback);  
       previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
       previewHolder.setFixedSize(getWindow().getWindowManager()  
         .getDefaultDisplay().getWidth(), getWindow().getWindowManager() 
         .getDefaultDisplay().getHeight()); 


      btnFlash = (ImageButton) findViewById(R.id.btnFlash);    
      /* 
      * First check if device is supporting flashlight or not 
      */ 
      hasFlash = getApplicationContext().getPackageManager() 
        .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 

      if (!hasFlash) { 
       // device doesn't support flash 
       // Show alert message and close the application 
       AlertDialog alert = new AlertDialog.Builder(CameraLauncherActivity.this) 
         .create(); 
       alert.setTitle("Error"); 
       alert.setMessage("Sorry, your device doesn't support flash light!"); 
       alert.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // closing the application 
         finish(); 
        } 
       }); 
       alert.show(); 
       return;  
      } 

      // displaying button image 
      toggleButtonImage(); 

      btnFlash.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (isFlashOn) { 
         // turn off flash 
         turnOffFlash(); 
        } else { 
         // turn on flash 
         turnOnFlash(); 
        }     
       } 
      });      
    }  

/* 
* Turning On flash 
*/ 
private void turnOnFlash() { 
    if (!isFlashOn) { 
     if (camera == null || params == null) { 
      return; 
     } 

     params = camera.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_ON); 
     camera.setParameters(params); 
     isFlashOn = true; 

     // changing button/switch image 
     toggleButtonImage(); 
    } 
} 

/* 
* Turning Off flash 
*/ 
private void turnOffFlash() { 
    if (isFlashOn) { 
     if (camera == null || params == null) { 
      return; 
     } 

     params = camera.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_OFF); 
     camera.setParameters(params); 
     isFlashOn = false; 

     // changing button/switch image 
     toggleButtonImage(); 
    } 
} 

/* 
* Toggle switch button images 
* changing image states to on/off 
* */ 
private void toggleButtonImage(){ 
    if(isFlashOn){ 
     btnFlash.setImageResource(R.drawable.green_button); 
    }else{ 
     btnFlash.setImageResource(R.drawable.red_button); 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    camera=Camera.open(); 
    } 

@Override 
public void onPause() { 
    super.onPause(); 
    if (inPreview) { 
    camera.stopPreview(); } 
    camera.release(); 
    camera=null; 
    inPreview=false;   
    } 


private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters parameters){ 
    Camera.Size result=null; 
    for (Camera.Size size : parameters.getSupportedPreviewSizes()) 
    { 
     if (size.width<=width && size.height<=height) 
     { 
      if (result==null) { 
       result=size; 
      } else { 
       int resultArea=result.width*result.height; 
       int newArea=size.width*size.height; 
       if (newArea>resultArea) { 
        result=size; 
        } 
       } 
      } 
     } 
    return(result); 
    }  
    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback(){ 

    public void surfaceCreated(SurfaceHolder holder) {  
     try {   
      camera.setPreviewDisplay(previewHolder); 
      } catch (Throwable t) { 
       Log.e("PreviewDemo-surfaceCallback", 
         "Exception in setPreviewDisplay()", t); 
       Toast.makeText(CameraLauncherActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();  
       }  
      }  

    public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) { 
     params = camera.getParameters();  
     params.setFlashMode(Camera.Parameters.FLASH_MODE_ON); 
     Camera.Size size=getBestPreviewSize(width, height,           
       params);  
     if (size!=null) {  
      params.setPreviewSize(size.width, size.height); 
      camera.setParameters(params);  
      camera.startPreview();  
      inPreview=true; 
      ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls); 

      if (params.isZoomSupported()) { 
       final int maxZoomLevel = params.getMaxZoom(); 
       Log.i("max ZOOM ", "is " + maxZoomLevel); 
       zoomControls.setIsZoomInEnabled(true); 
       zoomControls.setIsZoomOutEnabled(true); 

       zoomControls.setOnZoomInClickListener(new OnClickListener(){ 
        public void onClick(View v){ 
         if(currentZoomLevel < maxZoomLevel){ 
          currentZoomLevel++; 
          //mCamera.startSmoothZoom(currentZoomLevel); 
          params.setZoom(currentZoomLevel); 
          camera.setParameters(params); 
         } 
        } 
       }); 

       zoomControls.setOnZoomOutClickListener(new OnClickListener(){ 
        public void onClick(View v){ 
         if(currentZoomLevel > 0){ 
          currentZoomLevel--; 
          params.setZoom(currentZoomLevel); 
          camera.setParameters(params); 
         } 
        } 
       });  
      } 
      else 
       zoomControls.setVisibility(View.GONE); 
      }  
     }  

    public void surfaceDestroyed(SurfaceHolder holder) { 

     } 
    };    
    } 
+0

您的設備可能有多個攝像頭(例如背面朝前),但並非全部都有閃光燈。迭代所有相機,直到你找到一個支持閃光燈,並打開相機 – yoah

+0

@yoah我使用的是Galaxy EK-GC100相機,它只有後置相機 – Sun

回答

9

有幾件事情,我能想到的時,閃光燈會顯示:

請記住添加功能a第二對清單要求的相機應用:

<!-- Camera Requirement --> 
<uses-feature android:name="android.hardware.camera" /> 

<!-- Camera Permissions --> 
<uses-permission android:name="android.permission.CAMERA" />  

<!-- Features --> 
<uses-feature android:name="android.hardware.camera.flash" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 

如果你想使用它作爲手電筒,你可以做什麼@yoah說:

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH) 

如果沒有,打開閃光燈上會出來的時候你拍照,你可以使用:

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON) 

我最近寫了一個自定義的相機應用,但還沒有上傳到Github的是,它擁有所有工作的基本功能。

+0

嗨@AbrahimNeil,我剛剛將它上傳到Github:https:// github.com/agustinjuarez/android-camera-api,這是一個基本的WIP,所以它可能包含很多bug,並且很少有文檔,但它可能足以讓你篡改。 – CodingDuckling

+0

謝謝@CodingDuckling :) – Sun

2

嘗試使用

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH) 

設置FLASH_MODE_ON意味着拍照