2017-03-03 206 views
1

我試圖記錄採用Android Camera2 API的視頻。我試圖通過在請求生成器設置SCALER_CROP_REGION裁剪視頻作爲一個正方形。我使用下面的代碼,但它似乎沒有工作的Android Camera2 API裁剪視頻

mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { 

    @Override 
    public void onConfigured(CameraCaptureSession cameraCaptureSession) { 
     mCaptureSession = cameraCaptureSession; 
     try { 
      mIsRecording = true; 

      /////****** this where i'm setting the coping 
      mZoom = getZoomRect(); 
      mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, mZoom); 
      /////////****************** 
      mPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); 
      HandlerThread thread = new HandlerThread("CameraPreview"); 
      thread.start(); 
      mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, mBackgroundHandler); 
     } catch (CameraAccessException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { 
     Log.d(TAG, "onConfigureFailed"); 
    }}, mBackgroundHandler); 

這是應該得到的區域可以裁剪

public int zoom_level = 1; 
public Rect mZoom = null; 

public Rect getZoomRect(){ 
    try { 
     CameraManager manager = (CameraManager) getApplicationContext().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 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; 
     mZoom = new Rect(cropW, cropH, m.width() - cropW, m.height() - cropH); 
     ///// if recording video make it square 
     if (mIsRecording) { 
      mZoom = new Rect(cropW, cropH, m.width() - cropW, m.width() - cropW); 
     } 


    } catch (CameraAccessException e) { 
     Log.e(TAG, "can not access camera",e); 
     throw new RuntimeException("can not access camera.", e); 
    } catch (NullPointerException ex) { 
     Log.e(TAG, "touch logic",ex); 
    } 

    return mZoom; 
} 
+0

到底是什麼問題? –

+0

設置SCALER_CROP_REGION不會改變最終的視頻東西。 –

回答

1

假設zoom_level = 1的代碼,SCALER_AVAILABLE_MAX_DIGITAL_ZOOM是4,然後你最終

minW = m.width/40 
minH = m.height/40 
difW = m.width*39/40 
difH = m.height*39/40 
cropW = m.width*39/40/100 * 1 
cropH = m.height*39/40/100 * 1 
(floor cropW/cropH down to nearest multiple of 4) 

如果m.width = 3000,m.width = 2000,然後cropW = 28,cropH = 16

所以mZoom記錄是

Rect(28, 16, 2972, 2972). 

這是不集中,許多設備只支持中心變焦。但至少它是一個廣場區域。

真正的問題是,你不能改變裁剪輸出的寬高比 - 長寬比由輸出表面的分辨率當你創建一個捕獲會話固定。請參閱these diagrams瞭解種植方式。