我正在使用cameraKit处理我的相机活动。我现在的问题是有时它可以正常工作,而其他时候则根本无法工作。 正如您在下面的代码中看到的,我插入了一堆日志记录。有时它将遍历所有日志,有时只打印“单击按钮的按钮”。
无法弄清为什么不一致:
*为了不进行任何检查,我尝试将其运行和重新运行约0次,而且不一致。
class CameraFragment : Fragment() {
lateinit var cameraKitView: CameraKitView
private lateinit var localImageViewModel: LocalImageViewModel
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.let {
localImageViewModel = ViewModelProviders.of(this).get(LocalImageViewModel::class.java)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(co.getdere.R.layout.fragment_camera, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mActivity = activity as CameraActivity
cameraKitView = view.findViewById(co.getdere.R.id.camera_view)
cameraKitView.aspectRatio = 0.8f
val captureButton = view.findViewById<ImageButton>(co.getdere.R.id.camera_btn)
captureButton.setOnClickListener {
Log.d("photoActivity", "button clicked")
cameraKitView.captureImage { _, image ->
Log.d("photoActivity", "image captured")
val timeStamp = System.currentTimeMillis().toString()
val fileName = "Dere$timeStamp.jpg"
val path =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + "Dere"
val outputDir = File(path)
outputDir.mkdir()
val savedPhoto = File(path + File.separator + fileName)
Log.d("photoActivity", "new file created")
try {
val outputStream = FileOutputStream(savedPhoto.path)
outputStream.write(image)
outputStream.close()
mActivity.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(savedPhoto)))
Log.d("photoActivity", "Image saved to file and system rescaned the device")
Glide.with(this).load(savedPhoto).into(mActivity.photoEditorFragment.view!!.findViewById(R.id.photo_editor_image))
Log.d("photoActivity", "image loaded into new fragment")
mActivity.switchVisibility(1)
Log.d("photoActivity", "visibility switched")
val localImagePost = LocalImagePost(2343, savedPhoto.path)
Log.d("photoActivity", "Took photo")
localImageViewModel.insert(localImagePost)
} catch (e: java.io.IOException) {
e.printStackTrace()
Log.d("photoActivity", "failed to take photo")
}
}
}
}
override fun onStart() {
super.onStart()
cameraKitView.onStart()
}
override fun onResume() {
super.onResume()
cameraKitView.onResume()
}
override fun onPause() {
cameraKitView.onPause()
super.onPause()
}
override fun onStop() {
cameraKitView.onStop()
super.onStop()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onDetach() {
super.onDetach()
stopRepeatingTask()
}
companion object {
fun newInstance(): CameraFragment = CameraFragment()
}
}
答案 0 :(得分:0)
如果您将其用于其他应用程序,那么我确实为您提供建议。使用非测试版。完美运作。当前版本:v0.13.4 文件已经过时了。
新用法:
cameraKit.addCameraKitListener(object : CameraKitEventListener {
override fun onVideo(video: CameraKitVideo?) {
Log.i("camera", "onVideo")
}
override fun onEvent(event: CameraKitEvent?) {
Log.i("camera", "onEvent: $event")
}
override fun onImage(capturedImage: CameraKitImage?) {
Log.i("camera", "onImage")
capturedImage?.let { image ->
cameraViewModel.saveAndCompressImage(image.jpeg)
}
}
override fun onError(e: CameraKitError?) {
Log.i("camera", "onError: ${e?.exception}")
}
})