2017-07-25 124 views
1

更新2:任何對焦模式顯然不工作自動對焦模式下無法工作在三星S8

更新1:問題似乎是獨立於我的三星S8設備。它正在使用Nexus 5X和小米手機

我一直在努力使FOCUS_MODE_CONTINUOUS_PICTURE工作,但無濟於事。我已確認我的設備通過getSupportedFocusModes()支持連續自動圖片,但不確定爲什麼它不起作用。

這裏是我的代碼,連續自動對焦在startCamera()被設置:

class CaptureReceiptFragment : Fragment(), CaptureReceiptContract.View { 

private val MY_PERMISSIONS_REQUEST_CAMERA = 1 

private var camera: Camera? = null 
private var hasCameraPermission: Boolean? = null 

private lateinit var preview: CameraPreview 

override var presenter: CaptureReceiptContract.Presenter? = null 

private val picture = Camera.PictureCallback { data, camera -> 

    val pictureStorageDir = File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), getAppName()) 

    presenter?.saveReceipt(pictureStorageDir, data) 


} 

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, 
          savedInstanceState: Bundle?): View? { 
    val root = inflater!!.inflate(R.layout.capturereceipt_frag, container, false) 

    return root 
} 

override fun onActivityCreated(savedInstanceState: Bundle?) { 
    super.onActivityCreated(savedInstanceState) 

    hasCameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED 

    if (hasCameraPermission!!) { 
     showCameraErrorMsg() 
    } else { 
     capturereceipt_button_capturereceipt.setOnClickListener { 
      captureReceipt() 
     } 
     startCameraOnCreate() 
    } 
} 

override fun onResume() { 
    super.onResume() 
    presenter?.start() 
    if (hasCameraPermission!!) { 
     showCameraErrorMsg() 
    } else { 
     startCamera() 
     setCameraRotation() 
    } 
} 

override fun onPause() { 
    super.onPause() 
    camera?.release() 
    camera = null 
} 

override fun gotoReceiptList() { 
    //TODO 
} 

override fun goBack() { 
    //TODO 
} 

private fun startCameraOnCreate() { 
    preview = CameraPreview(context) 
    capturereceipt_framelayout_viewfinder.addView(preview) 
    startCamera() 
} 

private fun startCamera() { 
    if (camera == null) { 
     camera = Camera.open() 
     if (camera == null) { 
      showCameraErrorMsg() 
     } else { 
      val params: Camera.Parameters? = camera!!.parameters 
      if (params!!.getSupportedFocusModes() 
        .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { 
       params.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE; 
      } 
      camera?.parameters = params 
      preview.camera = camera 
      capturereceipt_textview_cameraerrormsg.visibility = View.GONE 
      capturereceipt_framelayout_viewfinder.visibility = View.VISIBLE 
     } 
    } 
} 

private fun showCameraErrorMsg() { 
    capturereceipt_textview_cameraerrormsg.visibility = View.VISIBLE 
    capturereceipt_framelayout_viewfinder.visibility = View.GONE 
} 

override fun captureReceipt() { 
    camera?.takePicture(null, null, picture) 
} 

private fun getOutputPictureFile(): File? { 
    val appName = getAppName() 

    val pictureStorageDir = File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), appName) 

    if (!pictureStorageDir.exists()) { 
     pictureStorageDir.mkdirs() 
    } 

    val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date()) 
    return File(pictureStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg") 
} 

private fun getAppName(): String { 
    val appInfo = context.getApplicationInfo() 
    val stringId = appInfo.labelRes 
    if (stringId == 0) { 
     return appInfo.nonLocalizedLabel.toString() 
    } else { 
     return this.getString(stringId) 
    } 
} 

fun setCameraDisplayOrientation() { 
    val info = Camera.CameraInfo() 
    Camera.getCameraInfo(CAMERA_FACING_BACK, info) 
    val rotation = activity.windowManager.defaultDisplay.rotation 
    var degrees = 0 
    when (rotation) { 
     Surface.ROTATION_0 -> degrees = 0 
     Surface.ROTATION_90 -> degrees = 90 
     Surface.ROTATION_180 -> degrees = 180 
     Surface.ROTATION_270 -> degrees = 270 
    } 
    var result: Int 
    if (info.facing == CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360 
     result = (360 - result) % 360 // compensate the mirror 
    } else { // back-facing 
     result = (info.orientation - degrees + 360) % 360 
    } 
    camera?.setDisplayOrientation(result); 
} 

fun setCameraRotation() { 
    val info: Camera.CameraInfo = Camera.CameraInfo() 
    Camera.getCameraInfo(CAMERA_FACING_BACK, info) 
    val rotation = activity.windowManager.defaultDisplay.rotation 
    var degrees = 0 
    when(rotation) { 
     Surface.ROTATION_0 -> degrees = 0 
     Surface.ROTATION_90 -> degrees = 90 
     Surface.ROTATION_180 -> degrees = 180 
     Surface.ROTATION_270 -> degrees = 270 
    } 
    val rotate = (info.orientation - degrees + 360) % 360 
    camera?.setDisplayOrientation(rotate) 
    val params: Camera.Parameters = camera!!.parameters 
    params.setRotation(rotate) 
    camera?.parameters = params 
} 

companion object { 
    @JvmStatic fun newInstance(): CaptureReceiptFragment { 
     return CaptureReceiptFragment() 
    } 
} 

}

任何幫助,將不勝感激。謝謝!

+1

爲什麼downvotes?這似乎是一個合法的問題 – Vaiden

+0

https://stackoverflow.com/questions/25321968/trouble-with-focus-mode-continuous-picture-on-galaxy-s5相關? – Michael

+0

@Michael是的,雖然解決方案無效,但相關。我已經知道了;將在這裏發佈答案 –

回答

1

我終於想通了 - 自動對焦模式參數應該在三星S8中設置不同。

這裏是我試過失敗:

  1. 設置任何自動對焦模式(自動,CONTINUOUS_PICTURE,INFINITY) 開始預覽
  2. 計劃camera.autoFocus()運行每 1秒immdediately camera.open
  3. 前後

下面是我嘗試過的工作:

  1. 把連續對焦或camera.autoFocus()在按鈕的onClick
  2. 延遲的持續關注1秒設置後camera.open() < - 這就是解決了我的問題

我的代碼#2:

private val autoFocusExecutor = ScheduledThreadPoolExecutor(1) 

fun startCamera() { 
    if (camera == null) { 
     camera = Camera.open() 
     if (camera == null) { 
      showCameraErrorMsg() 
     } else { 
      preview.camera = camera 
      capturereceipt_textview_cameraerrormsg.visibility = View.GONE 
      capturereceipt_framelayout_viewfinder.visibility = View.VISIBLE 

      autoFocusExecutor.schedule(Sc{ 
       val params: Camera.Parameters = camera!!.parameters 
       if (params.getSupportedFocusModes() 
         .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { 
        //TODO: Auto focus not working 
        params.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE 
       } 
       camera?.parameters = params 
      }, 1000, TimeUnit.MILLISECONDS) 
     } 
    } 
} 
相關問題