Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JPEG_R capture to extension sample camera app #584

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add JPEG_R capture to extension sample camera app
  • Loading branch information
r-dhanjal committed Jul 9, 2024
commit 3618590d1eba898ee2442aa46e532637e345dcfb
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,21 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
extensionCharacteristics = cameraManager.getCameraExtensionCharacteristics(args.cameraId)
characteristics = cameraManager.getCameraCharacteristics(args.cameraId)
lensFacing = characteristics[CameraCharacteristics.LENS_FACING]!!
supportedExtensions.addAll(extensionCharacteristics.supportedExtensions)

if (args.jpegR) {
for (extension in extensionCharacteristics.supportedExtensions) {
val jpegRSizes = extensionCharacteristics.getExtensionSupportedSizes(
extension, ImageFormat.JPEG_R
)

if (jpegRSizes.isNotEmpty()) {
supportedExtensions.add(extension)
}
}
} else {
supportedExtensions.addAll(extensionCharacteristics.supportedExtensions)
}

if (currentExtension == -1) {
currentExtension = supportedExtensions[0]
currentExtensionIdx = 0
Expand Down Expand Up @@ -651,6 +665,7 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
previewSurface = createPreviewSurface(previewSize)
stillImageReader = createStillImageReader()
postviewImageReader = createPostviewImageReader()
isPostviewAvailable = postviewImageReader != null

val outputConfig = ArrayList<OutputConfiguration>()
outputConfig.add(OutputConfiguration(stillImageReader.surface))
Expand Down Expand Up @@ -722,14 +737,27 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
* Creates the still image reader and sets up OnImageAvailableListener
*/
private fun createStillImageReader(): ImageReader {
var stillFormat: Int
var stillCaptureSize: Size

val yuvColorEncodingSystemSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.YUV_420_888
)
val jpegSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.JPEG
)
val stillFormat = if (jpegSizes.isEmpty()) ImageFormat.YUV_420_888 else ImageFormat.JPEG
val stillCaptureSize = if (jpegSizes.isEmpty()) yuvColorEncodingSystemSizes[0] else jpegSizes[0]
stillFormat = if (jpegSizes.isEmpty()) ImageFormat.YUV_420_888 else ImageFormat.JPEG
stillCaptureSize = if (jpegSizes.isEmpty()) yuvColorEncodingSystemSizes[0] else jpegSizes[0]

if (Build.VERSION.SDK_INT >= 35) {
val jpegRSizes = extensionCharacteristics.getExtensionSupportedSizes(
currentExtension, ImageFormat.JPEG_R
)
if (args.jpegR && jpegRSizes.isNotEmpty()) {
stillFormat = ImageFormat.JPEG_R
stillCaptureSize = jpegRSizes[0]
}
}
val stillImageReader = ImageReader.newInstance(
stillCaptureSize.width,
stillCaptureSize.height, stillFormat, 1
Expand All @@ -742,7 +770,8 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
hideCaptureProgressUI()
val file = File(
requireActivity().getExternalFilesDir(null),
if (image.format == ImageFormat.JPEG) "frame.jpg" else "frame.yuv"
if (image.format == ImageFormat.JPEG
|| image.format == ImageFormat.JPEG_R) "frame.jpg" else "frame.yuv"
)
output = FileOutputStream(file)
output.write(getDataFromImage(image))
Expand Down Expand Up @@ -787,12 +816,14 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
)
val postviewSize: Size
val postviewFormat: Int
if (!jpegSupportedSizes.isEmpty()) {
if (jpegSupportedSizes.isNotEmpty()) {
postviewSize = jpegSupportedSizes[0]
postviewFormat = ImageFormat.JPEG
} else {
} else if (yuvSupportedSizes.isNotEmpty()){
postviewSize = yuvSupportedSizes[0]
postviewFormat = ImageFormat.YUV_420_888
} else {
return null
jsaund marked this conversation as resolved.
Show resolved Hide resolved
}
val postviewImageReader =
ImageReader.newInstance(postviewSize.width, postviewSize.height, postviewFormat, 1)
Expand Down Expand Up @@ -1235,7 +1266,7 @@ class CameraFragment : Fragment(), TextureView.SurfaceTextureListener {
val planes = image.planes
var buffer: ByteBuffer
var offset = 0
if (format == ImageFormat.JPEG) {
if (format == ImageFormat.JPEG || format == ImageFormat.JPEG_R) {
buffer = planes[0].buffer
data = ByteArray(buffer.limit())
buffer.rewind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package com.example.android.camera2.extensions.fragments

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.ImageFormat;
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -61,7 +63,7 @@ class SelectorFragment : Fragment() {
view.setOnClickListener {
Navigation.findNavController(requireActivity(), R.id.fragment_container)
.navigate(SelectorFragmentDirections.actionSelectorToCamera(
item.cameraId))
item.cameraId, item.jpegR))
}
}
}
Expand All @@ -71,7 +73,8 @@ class SelectorFragment : Fragment() {

private data class CameraInfo(
val name: String,
val cameraId: String)
val cameraId: String,
val jpegR: Boolean = false)

/** Converts a lens orientation enum into a human-readable string */
private fun lensOrientationString(value: Int) = when (value) {
Expand Down Expand Up @@ -99,9 +102,21 @@ class SelectorFragment : Fragment() {
.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) &&
extensionCharacteristics.supportedExtensions.isNotEmpty()) {
availableCameras.add(CameraInfo("$orientation ($id)", id))

if (Build.VERSION.SDK_INT >= 35) {
for (extension in extensionCharacteristics.supportedExtensions) {
val jpegRSizes = extensionCharacteristics.getExtensionSupportedSizes(
extension, ImageFormat.JPEG_R
)

if (jpegRSizes.isNotEmpty()) {
availableCameras.add(CameraInfo("$orientation ($id) JPEG_R", id, true))
break // Exit the loop since we found a suitable extension
}
}
}
}
}

return availableCameras
}
}
Expand Down
5 changes: 5 additions & 0 deletions Camera2Extensions/app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
app:argType="string"
app:nullable="false"/>

<argument
android:name="jpegR"
app:argType="boolean"
app:nullable="false"/>

</fragment>

</navigation>
Loading