簡體   English   中英

使用 cameraX 的 PreviewView 捕獲疊加

[英]capture overlay using PreviewView of cameraX

我正在嘗試使用圖像捕獲中包含的疊加來捕獲圖片。 我能夠使用cameraView.overlay.add(binding.textView)將覆蓋設置為previewView 但是,嘗試使用imageCapture保存圖像時它沒有保存,僅保存了圖片而不是疊加層。 如何使用相機 x 的PreviewView保存包含疊加的圖像。

請不要將此標記為重復。 我研究了很多,大多數在線示例都使用舊的camera api,它不適用於相機 x 庫。 任何幫助表示贊賞。 提前致謝。

這是我的代碼


           <FrameLayout
                android:id="@+id/camera_wrapper"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="@id/space1"
                app:layout_constraintBottom_toBottomOf="@id/space">
    
                <androidx.camera.view.PreviewView
                    android:id="@+id/camera_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <TextView
                    android:id="@+id/text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Hello world"
                    android:textSize="42sp"
                    android:textColor="@android:color/holo_green_dark"/>
    
            </FrameLayout>

  private lateinit var outputDirectory: File
  private lateinit var cameraExecutor: ExecutorService
  private var preview: Preview? = null
  private var lensFacing: Int = CameraSelector.LENS_FACING_FRONT
  private var imageCapture: ImageCapture? = null
  private var camera: Camera? = null
  private var cameraProvider: ProcessCameraProvider? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    outputDirectory = getOutputDirectory()
    cameraExecutor = Executors.newSingleThreadExecutor()
  }

  private fun setupCamera() {
    val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())

    cameraProviderFuture.addListener(
      Runnable {
        // Used to bind the lifecycle of cameras to the lifecycle owner
        cameraProvider = cameraProviderFuture.get()

        // Get screen metrics used to setup camera for full screen resolution
        val metrics = DisplayMetrics().also { binding.cameraView.display.getRealMetrics(it) }
        Timber.d("Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

        val screenAspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)
        Timber.d("Preview aspect ratio: $screenAspectRatio")

        val rotation = binding.cameraView.display.rotation

        // CameraProvider
        val cameraProvider = cameraProvider
          ?: throw IllegalStateException("Camera initialization failed.")

        // CameraSelector
        val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()

        // add text overlay *---------*
        binding.cameraView.overlay.add(binding.textView)

        // Preview
        preview = Preview.Builder()
          // We request aspect ratio but no resolution
          .setTargetAspectRatio(screenAspectRatio)
          // Set initial target rotation
          .setTargetRotation(rotation)
          .build()

        // ImageCapture
        imageCapture = ImageCapture.Builder()
          .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
          // We request aspect ratio but no resolution to match preview config, but letting
          // CameraX optimize for whatever specific resolution best fits our use cases
          .setTargetAspectRatio(screenAspectRatio)
          // Set initial target rotation, we will have to call this again if rotation changes
          // during the lifecycle of this use case
          .setTargetRotation(rotation)
          .build()

        // Must unbind the use-cases before rebinding them
        cameraProvider.unbindAll()

        try {
          // A variable number of use-cases can be passed here -
          // camera provides access to CameraControl & CameraInfo
          camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture)
          // Attach the viewfinder's surface provider to preview use case
          preview?.setSurfaceProvider(binding.cameraView.surfaceProvider)
        } catch (exc: Exception) {
          Toast.makeText(requireContext(), "Something went wrong. Please try again.", Toast.LENGTH_SHORT).show()
          findNavController().navigateUp()
        }
      },
      ContextCompat.getMainExecutor(requireContext())
    )
  }

private fun takePhoto() {
    imageCapture?.let { imageCapture ->

      // Create output file to hold the image
      val photoFile = createFile(outputDirectory, FILENAME, PHOTO_EXTENSION)

      // Setup image capture metadata
      val metadata = ImageCapture.Metadata().apply {

        // Mirror image when using the front camera
        isReversedHorizontal = lensFacing == CameraSelector.LENS_FACING_FRONT
      }

      // Create output options object which contains file + metadata
      val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile)
          .setMetadata(metadata)
          .build()

      // Setup image capture listener which is triggered after photo has been taken
      imageCapture.takePicture(outputOptions, cameraExecutor, object : ImageCapture.OnImageSavedCallback {
        override fun onError(exc: ImageCaptureException) {
          Timber.e(exc, "Photo capture failed: ${exc.message}")
        }

        override fun onImageSaved(output: ImageCapture.OutputFileResults) {
          val savedUri = output.savedUri ?: Uri.fromFile(photoFile)
          Timber.d("Photo capture succeeded: $savedUri")

          // Implicit broadcasts will be ignored for devices running API level >= 24
          // so if you only target API level 24+ you can remove this statement
          if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            requireActivity()
                .sendBroadcast(Intent(android.hardware.Camera.ACTION_NEW_PICTURE, savedUri))
          }


          // If the folder selected is an external media directory, this is
          // unnecessary but otherwise other apps will not be able to access our
          // images unless we scan them using [MediaScannerConnection]
          val mimeType = MimeTypeMap.getSingleton()
              .getMimeTypeFromExtension(savedUri.toFile().extension)
          MediaScannerConnection.scanFile(
              context,
              arrayOf(savedUri.toFile().absolutePath),
              arrayOf(mimeType)
          ) { _, uri ->
            Timber.d("Image capture scanned into media store: $uri")
          }
        }
      })
    }

  }


您必須自己將文本覆蓋在圖像上。 我建議使用takePicture(Executor, ...)將 Jpeg 放入內存中; 然后,使用其中一個庫(不屬於 Android 框架,也不屬於 Jetpack)覆蓋您的文本,並將結果保存在文件中。

如果您可以在圖像質量上妥協,您可以在位圖畫布上繪制 Jpeg,然后在頂部繪制文本。

用這個插件https://github.com/huangyz0918/AndroidWM 。另外,如果你想自己構建這個可以幫助你參考。

簡單的用法如下

WatermarkText watermarkText = new WatermarkText(inputText)
            .setPositionX(0.5)
            .setPositionY(0.5)
            .setTextColor(Color.WHITE)
            .setTextFont(R.font.champagne)
            .setTextShadow(0.1f, 5, 5, Color.BLUE)
            .setTextAlpha(150)
            .setRotation(30)
            .setTextSize(20);

val bmFinal: Bitmap = WatermarkBuilder
                            .create(applicationContext, capturedImageBitmap)
                            .loadWatermarkText(watermarkText)
                            .watermark
                            .outputImage

// ##### Then save it #########

fun saveImage(bitmap: Bitmap, photoFile: File) {
    val output: OutputStream = FileOutputStream(photoFile)
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output)
    output.flush()
    output.close()
    Toast.makeText(
        this@MainActivity,
        "Imaged Saved at ${photoFile.absolutePath}",
        Toast.LENGTH_LONG
    ).show()
}

val photoFile = File(
            outputDirectory,
            SimpleDateFormat(
                FILENAME_FORMAT, Locale.US
            ).format(System.currentTimeMillis()) + ".jpg"
        )

saveImage(bmFinal, photoFile)

也許您可以使用 CameraSource 類並將您的預覽/疊加放在里面:

val cameraSource = CameraSource.Builder(requireContext(),FakeDetector()).build()
cameraSource.start(your_preview_overlay) 

在你有了 API 之后:

takePicture(CameraSource.ShutterCallback shutter, CameraSource.PictureCallback jpeg) 

相機源( https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource )用於檢測,但您可以創建一個假檢測器(沒有檢測到)。

如果您無法承受質量損失,@alexcohn 的答案是首選。 但是,如果質量不是什么大問題,那么您可以這樣做。

<FrameLayout
            android:id="@+id/camera_wrapper"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="@id/space1"
            app:layout_constraintBottom_toBottomOf="@id/space">

            <androidx.camera.view.PreviewView
                android:id="@+id/camera_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <ImageView
                android:id="@+id/selfie"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:visibility="gone"
                tools:visibility="visible"
                tools:background="@color/gray" />

            <ImageView
                android:id="@+id/overlay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                tools:src="@drawable/full_frame_gd" />

        </FrameLayout>

PreviewView具有內置功能,可為您提供預覽的位圖

val bitmap = binding.cameraView.bitmap
binding.selfie.setImageBitmap(bitmap)
binding.selfie.visibility = View.VISIBLE
cameraExecutor.shutdown()
binding.cameraView.visibility = View.GONE

現在您有兩張圖片查看,一張用於selfie ,一張用於overlay 您無法拍攝 previewView 的屏幕截圖。 它有一些限制,我不太確定。 但是,我相信可能有辦法解決它。

從這里您可以像這樣截取兩個組合圖像視圖的屏幕截圖

private fun captureView(view: View, window: Window, bitmapCallback: (Bitmap)->Unit) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      // Above Android O, use PixelCopy
      val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
      val location = IntArray(2)
      view.getLocationInWindow(location)
      PixelCopy.request(
          window,
          Rect(
              location[0],
              location[1],
              location[0] + view.width,
              location[1] + view.height
          ),
          bitmap,
          {
            if (it == PixelCopy.SUCCESS) {
              bitmapCallback.invoke(bitmap)
            }
          },
          Handler(Looper.getMainLooper()) )
    } else {
      val tBitmap = Bitmap.createBitmap(
          view.width, view.height, Bitmap.Config.RGB_565
      )
      val canvas = Canvas(tBitmap)
      view.draw(canvas)
      canvas.setBitmap(null)
      bitmapCallback.invoke(tBitmap)
    }
  }

在 takePhoto() 函數中,您可以刪除imageCapture.takePicture邏輯並將其替換為這個。

Handler(Looper.getMainLooper()).postDelayed({
        captureView(binding.cameraWrapper, requireActivity().window) {
          // your new bitmap with overlay is here and you can save it to file just like any other bitmaps.
        }
      }, 500)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM