2017-03-25 41 views
1

我寫了銀河S7的應用程序,它顯示在我想用控制縮放級別的實時預覽Camera2:Camera2標裁剪區域和傳感器圖像尺寸

captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCropPreview); 
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler); 

的矩形zoomCropPreview是針對所需的不同作物/變焦而改變。由於S7支持最大數字變焦8,其傳感器陣列尺寸爲4032x3024,因此我很難確定Rect值。我已經看過https://source.android.com/devices/camera/camera3_crop_reprocess.html的指導,但我卡住了。

例如,zoomCropPreview =(0,0,4032,3024)與(0,0,504,378) - 8倍變焦一樣工作得很好。但是其他區域(如250,250,504,378)或(1512,1134,1008,756)不起作用,即使它們的邊界在傳感器陣列內。這是基於https://inducesmile.com/android/android-camera2-api-example-tutorial/

protected void createCameraPreview() { 
     try { 
     SurfaceTexture texture = textureView.getSurfaceTexture(); 
     assert texture != null; 
     texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight()); 
     Surface surface = new Surface(texture); 
     captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 
     captureRequestBuilder.addTarget(surface); 
     cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() { 
      @Override 
      public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { 
       //The camera is already closed 
       if (null == cameraDevice) { 
        return; 
       } 
       // When the session is ready, we start displaying the preview. 
       //try to change zoom 
       captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCropPreview); 
       cameraCaptureSessions = cameraCaptureSession; 
       updatePreview();     //update preview screen 
      } 

      @Override 
      public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { 
       Toast.makeText(AndroidCameraApi.this, "Configuration change", Toast.LENGTH_SHORT).show(); 
      } 
     }, null); 
    } catch (CameraAccessException e) { 
     e.printStackTrace(); 
    } 
} 

代碼的相機預覽部分如何確定scalar_crop_region正確的矩形值?

+0

你找到了你的問題的答案?我也有一些麻煩http://stackoverflow.com/questions/43724447/camera-2-crop-region –

回答

0

如果你看看文檔爲SCALER_CROP_REGION它會告訴你

作物區域的寬度和高度不能設置爲比地面更小(activeArraySize.width/android.scaler.availableMaxDigitalZoom)和地板(activeArraySize.height/android.scaler.availableMaxDigitalZoom)。

該文檔還指出,它將根據硬件等進行輪迴 - 您要計算裁剪區域的高度和寬度。從這些項目,你可以計算左上角和右下角。這些角落將提供裁剪區域的面積。

float cropW = activeArraySize.width()/zoomLevel; 
float cropH = activeArraySize.height()/zoomLevel; 

// now we calculate the corners 
int top = activeArraySize.centerY() - (int) (cropH/2f); 
int left = activeArraySize.centerX() - (int) (cropW/2f); 
int right = activeArraySize.centerX() + (int) (cropW/2f); 
int bottom = activeArraySize.centerY() + (int) (cropH/2f); 

請記住,屏幕的左上角是0,0。畫出這個進一步的澄清