From 37d5c191e919fdaaecca8879362e9b73987fc9e9 Mon Sep 17 00:00:00 2001 From: dece Date: Sat, 2 May 2020 01:45:09 +0200 Subject: [PATCH] Add camera photo input --- app/src/main/AndroidManifest.xml | 2 + .../dev/lowrespalmtree/zmingz/MainActivity.kt | 76 +++++++++++++++---- app/src/main/res/layout/activity_main.xml | 22 ++++++ app/src/main/res/values/strings.xml | 7 +- 4 files changed, 91 insertions(+), 16 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 801d085..824142a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + handlePickResult(requestCode, resultCode, data) + REQ_TAKE_IMG -> handleTakeImageResult(resultCode) else -> super.onActivityResult(requestCode, resultCode, data) } } @@ -79,6 +96,13 @@ class MainActivity: AppCompatActivity() { super.onRequestPermissionsResult(requestCode, permissions, grantResults) } + private fun getUriForFile(file: File): Uri = + FileProvider.getUriForFile( + this, + "dev.lowrespalmtree.fileprovider", + file + ) + fun openFile(v: View) { val req: Int val pickIntent = when (v.id) { @@ -116,6 +140,26 @@ class MainActivity: AppCompatActivity() { menu.show() } + fun openCamera(v: View) { + if (!packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) { + Log.e(TAG, "No camera feature") + return + } + + when (v.id) { + buttonCamera.id -> { + val inputFile = File(mediaCacheDir, "${System.currentTimeMillis()}.jpg") + cameraImagePath = inputFile.canonicalPath + val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) + .apply { putExtra(MediaStore.EXTRA_OUTPUT, getUriForFile(inputFile)) } + startActivityForResult(captureIntent, REQ_TAKE_IMG) + } + buttonCameraVideo.id -> { + + } + } + } + private fun mediaTypeFromViewId(id: Int): MediaType? = when (id) { imageView1.id, imageView2.id -> MediaType.IMAGE @@ -125,6 +169,8 @@ class MainActivity: AppCompatActivity() { @Suppress("UNUSED_PARAMETER") private fun handlePickResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (resultCode != Activity.RESULT_OK) + return val uri = data?.data ?: return Unit.also { Log.e(TAG, "No intent or data") } val type = when (requestCode) { @@ -135,6 +181,13 @@ class MainActivity: AppCompatActivity() { processMedia(uri, type) } + private fun handleTakeImageResult(resultCode: Int) { + if (resultCode != Activity.RESULT_OK) + return + processMedia(cameraImagePath, MediaType.IMAGE) + } + + /** Process media at URI, copying to a local cache file for FFmpeg beforehand. */ private fun processMedia(uri: Uri, type: MediaType) { val uriPath = uri.path ?: return Unit.also { Log.e(TAG, "No path in URI") } @@ -149,23 +202,22 @@ class MainActivity: AppCompatActivity() { } } - val mediaCacheDir = File(cacheDir, "media") - if (!mediaCacheDir.exists() && !mediaCacheDir.mkdir()) { - Log.e(TAG, "Could not create cache media dir") - Toast.makeText(this, R.string.write_error, Toast.LENGTH_SHORT).show() - return - } + processMedia(inputFile.canonicalPath, type) + } + /** Start the async mirroring tasks. */ + private fun processMedia(path: String, type: MediaType) { imageLayout.visibility = View.GONE videoLayout.visibility = View.GONE Toast.makeText(this, R.string.please_wait, Toast.LENGTH_SHORT).show() + val extension = getFileExtension(path) val outputFile1 = File.createTempFile("output1", ".$extension", mediaCacheDir) MirrorTask(WeakReference(this), type, 1) - .execute(inputFile.canonicalPath, outputFile1.canonicalPath, VF1) + .execute(path, outputFile1.canonicalPath, VF1) val outputFile2 = File.createTempFile("output2", ".$extension", mediaCacheDir) MirrorTask(WeakReference(this), type, 2) - .execute(inputFile.canonicalPath, outputFile2.canonicalPath, VF2) + .execute(path, outputFile2.canonicalPath, VF2) } class MirrorTask( @@ -267,11 +319,7 @@ class MainActivity: AppCompatActivity() { } private fun share(path: String, type: MediaType) { - val uri = FileProvider.getUriForFile( - this, - "dev.lowrespalmtree.fileprovider", - File(path) - ) + val uri = getUriForFile(File(path)) Log.i(TAG, "share with uri $uri") val shareIntent = Intent().also { it.action = Intent.ACTION_SEND @@ -289,6 +337,8 @@ class MainActivity: AppCompatActivity() { private const val REQ_PICK_IMG = 1 private const val REQ_PICK_VID = 2 private const val REQ_WRITE_PERM = 3 + private const val REQ_TAKE_IMG = 4 + private const val REQ_TAKE_VID = 5 private const val VF1 = "crop=iw/2:ih:0:0,split[left][tmp];[tmp]hflip[right];[left][right] hstack" private const val VF2 = "crop=iw/2:ih:iw/2:0,split[left][tmp];[tmp]hflip[right];[right][left] hstack" diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 59383ee..45e459d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -83,6 +83,28 @@ app:layout_constraintStart_toStartOf="parent" style="@style/Widget.AppCompat.ButtonBar"> + + + +