M E R A <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
M E R A <com.otaliastudios.cameraview.CameraView android:id="@+id/cameraView" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" app:cameraFacing="back|front" app:cameraGestureTap="none|autoFocus|takePicture" app:cameraGesturePinch="none|zoom|exposureCorrection" app:cameraUseDeviceOrientation="false" ... />
M C A M E R A cameraView.addCameraListener(object : CameraListener() { @UiThread override fun onPictureTaken(result: PictureResult) { @WorkerThread result.toBitmap(object : BitmapCallback { @UiThread override fun onBitmapReady(bitmap: Bitmap?) { ... } }) } }) cameraView.takePicture() Bitmap
M C A M E R A cameraView.addFrameProcessor(object : FrameProcessor { @WorkerThread override fun process(frame: Frame) { val data: ByteArray = frame.data val width: Int = frame.size.width val height: Int = frame.size.height val format: Int = frame.format // android.graphics.ImageFormat val rotation: Int = frame.rotation // 0, 90, 180, 270 } }) ByteArray
M C A M E R A cameraView.removeCameraListener(...) cameraView.removeFrameProcessor(...) // or cameraView.clearCameraListeners() cameraView.clearFrameProcessors()
R O M C A M E R A class ... : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) cameraView.setLifecycleOwner(this) // Add Listeners } override fun onDestroy() { // Remove Listeners super.onDestroy() } }
R O M C A M E R A class ... : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) cameraView.setLifecycleOwner(viewLifecycleOwner) // Add Listeners } override fun onDestroyView() { // Remove Listeners super.onDestroyView() } }
A L L E R Y galleryButton.setOnClickListener { Gallery.takePicture(activity) } override fun onActivityResult(requestCode: Int, ...) { super.onActivityResult(requestCode, resultCode, data) Gallery.onPictureTaken(requestCode, resultCode, data) { uri -> … } } Link: https://developer.android.com/guide/components/intents-common#Storage
Intent F R O M G A L L E R Y fun takePicture(activity: Activity) { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.addCategory(Intent.CATEGORY_OPENABLE) intent.type = "image/*" activity.startActivityForResult( Intent.createChooser(intent, "Select Picture"), REQUEST_GET_SINGLE_FILE /* 1234 */ ) }
F R O M G A L L E R Y fun onPictureTaken( requestCode: Int, resultCode: Int, data: Intent?, callback: (Uri) -> Unit) { if (requestCode == REQUEST_GET_SINGLE_FILE && resultCode == Activity.RESULT_OK) { data?.data?.run(callback) } } URI
I T M A P implementation 'com.google.firebase:firebase-ml-vision:22.0.0' Link: https://firebase.google.com/.../firebase/ml/vision/common/FirebaseVisionImage
O B I T M A P val bitmap = FirebaseVisionImage.fromByteArray( frame.data, // ByteArray FirebaseVisionImageMetadata.Builder() .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setWidth(frame.size.width) .setHeight(frame.size.height) .setRotation(FirebaseVisionImageMetadata.ROTATION_90) .build()) .bitmap
A L S T O R A G E fun saveBitmap(context: Context, bitmap: Bitmap) { val imageTime = System.currentTimeMillis() val imageDate = SimpleDateFormat("yyyyMMdd-HHmmss").format(Date(imageT val imageFileName = String.format("Stylize_%s.png", imageDate) val imageDir = File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), DIR_NAME) val imageFilePath = File("Stylizes", imageFileName).absolutePath ... Link: http://androidxref.com/9.0.0_r3/xref/.../SystemUI/.../GlobalScreenshot.java
A L S T O R A G E ... try { imageDir.mkdirs() val out = FileOutputStream(imageFilePath) bitmap.compress(Bitmap.CompressFormat.PNG, 100, out) out.flush() out.close() } catch (IOException) { ... } } Link: http://androidxref.com/9.0.0_r3/xref/.../SystemUI/.../GlobalScreenshot.java