2016-03-13 157 views
6

Im新的android camera2 APi。 我只是將我的所有項目移動到新的Camera2 Api。我用Camera2Basic example作爲起點。android camera2手柄變焦

林現在加入這一努力手柄變焦:

public boolean onTouchEvent(MotionEvent event) { 
    try { 
     CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 
     CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId); 
     float maxZoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))*10; 

     Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); 
     int action = event.getAction(); 
     float current_finger_spacing; 

     if (event.getPointerCount() > 1) { 
      // Multi touch logic 
      current_finger_spacing = getFingerSpacing(event); 

      if(finger_spacing != 0){ 
       if(current_finger_spacing > finger_spacing && maxZoom > zoom_level){ 
        zoom_level++; 

       } 
       else if (current_finger_spacing < finger_spacing && zoom_level > 1){ 
        zoom_level--; 

       } 
       int minW = (int) (m.width()/maxZoom); 
       int minH = (int) (m.height()/maxZoom); 
       int difW = m.width() - minW; 
       int difH = m.height() - minH; 
       int cropW = difW /100 *(int)zoom_level; 
       int cropH = difH /100 *(int)zoom_level; 
       cropW -= cropW & 3; 
       cropH -= cropH & 3; 
       Rect zoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH); 
       mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom); 
      } 
      finger_spacing = current_finger_spacing; 
     } 
     else{ 
      if (action == MotionEvent.ACTION_UP) { 
       //single touch logic 
      } 
     } 

     try { 
      mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, 
        null); 
     } 
     catch (CameraAccessException e) { 
      e.printStackTrace(); 
     } 
     catch (NullPointerException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 
    catch (CameraAccessException e) 
    { 
     throw new RuntimeException("can not access camera.", e); 
    } 

    return true; 
} 

這:

private float getFingerSpacing(MotionEvent event) { 
    float x = event.getX(0) - event.getX(1); 
    float y = event.getY(0) - event.getY(1); 
    return FloatMath.sqrt(x * x + y * y); 
} 

但我拍攝後,畫面的結果是沒有變焦。 我怎樣才能讓它發生。 謝謝大家。

更新 需要補充captureBuilder.set(CaptureRequest.SCALER_CROP_REGION,縮放);捕獲StillPicture()方法。

+0

你解決了嗎? –

+0

是的,在下面添加了我的解決方案 –

+0

captureStillPicture()方法是什麼? – user3819810

回答

4

需要添加captureBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom); 要captureStillPicture()方法。

2

您只在CaptureRequestBuilder上設置SCALER_CROP_REGION以獲得定期預覽攝像頭輸出。您只需將相同的裁剪區域屬性添加到使用ImageSaver的JPEG Surface作爲輸出的CaptureRequestBuilder,並且應該全部設置。

+0

請問你能更特別嗎?它在哪種方法中顯示? –

+0

@EliElezra您必須在您捕獲照片時發送給照片的構建器中調用set。(CaptureRequest.SCALER_CROP_REGION)。該構建器包含有關您正在觀看的預覽和將要保存的照片的所有信息。您可以在構建器中修改預覽和照片設置參數。 您可以在以下位置查看更多信息:http://developer.android.com/intl/es/reference/android/hardware/camera2/CameraCharacteristics.html PD:我在下面給你發了一篇文章,看一看。 –

0

只需將SCALER.CROP.REGION設置爲captureBuilder即可。你可以這樣做:

yourCapturebuilder.(CaptureRequest.SCALER_CROP_REGION, newZoom); 

換句話說,如果你想保持放大的偏好。我建議你做這樣的事情:保存矩形的偏好作爲一個String,後來恢復它使用它,或者把它每次你打開攝像頭:

Preferences.edit().putString(CameraSettings.KEY_ZOOM,newZoom.toString()); 

後來調用此方法來設置縮放dinamically:

public boolean setZoomValue(CaptureRequest.Builder builder) { 
     Log.i(TAG,"zoom preference value " + mPreferences.getString(CameraSettings.KEY_ZOOM,null)); 
     String rawZoomValue = mPreferences.getString(CameraSettings.KEY_ZOOM, null); 
     if (rawZoomValue == null) 
      return false; 
     rawZoomValue = rawZoomValue.replaceAll("[Rect() ]", ""); 
     String[] rectZoomList = rawZoomValue.split(",|\\-|\\)|\\("); 
     Rect zoomValue = new Rect(Integer.parseInt(rectZoomList[0]), Integer.parseInt(rectZoomList[1]), Integer.parseInt(rectZoomList[2]),Integer.parseInt(rectZoomList[3])); 
     builder.set(CaptureRequest.SCALER_CROP_REGION, zoomValue); 
     Log.i(TAG, "Zoom applied: " + zoomValue); 
     return true; 

    } 

希望它會幫助你!