Skip to content
Snippets Groups Projects
Commit d8e026d5 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tv: handle cameras without 720 resolution

Change-Id: If23000fa49fded15c5770e3c74c9823fb871534b
parent 4f8cf61e
Branches
Tags nightly/20250509.0
No related merge requests found
...@@ -673,8 +673,7 @@ public class CameraService { ...@@ -673,8 +673,7 @@ public class CameraService {
Log.w(TAG, "supportedSize: " + option); Log.w(TAG, "supportedSize: " + option);
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight && if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
option.getHeight() == option.getWidth() * h / w) { option.getHeight() == option.getWidth() * h / w) {
if (option.getWidth() >= textureViewWidth && if (option.getWidth() >= textureViewWidth && option.getHeight() >= textureViewHeight) {
option.getHeight() >= textureViewHeight) {
bigEnough.add(option); bigEnough.add(option);
} else { } else {
notBigEnough.add(option); notBigEnough.add(option);
......
...@@ -32,6 +32,7 @@ import android.hardware.camera2.CameraManager.AvailabilityCallback ...@@ -32,6 +32,7 @@ import android.hardware.camera2.CameraManager.AvailabilityCallback
import android.os.Bundle import android.os.Bundle
import android.renderscript.* import android.renderscript.*
import android.util.Log import android.util.Log
import android.util.Size
import android.view.View import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageView import android.widget.ImageView
...@@ -55,6 +56,7 @@ import net.jami.model.Account ...@@ -55,6 +56,7 @@ import net.jami.model.Account
import net.jami.services.AccountService import net.jami.services.AccountService
import net.jami.services.DeviceRuntimeService import net.jami.services.DeviceRuntimeService
import net.jami.services.HardwareService import net.jami.services.HardwareService
import java.util.*
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
...@@ -167,9 +169,9 @@ class HomeActivity : FragmentActivity() { ...@@ -167,9 +169,9 @@ class HomeActivity : FragmentActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
if (mCameraPreview != null) { mCameraPreview?.let { preview ->
mCamera!!.setPreviewCallback(null) mCamera?.setPreviewCallback(null)
mCameraPreview!!.stop() preview.stop()
mCameraPreview = null mCameraPreview = null
} }
} }
...@@ -182,50 +184,81 @@ class HomeActivity : FragmentActivity() { ...@@ -182,50 +184,81 @@ class HomeActivity : FragmentActivity() {
camera.release(); camera.release();
mCamera = null mCamera = null
} }
if (mBlurOutputBitmap != null) { mBlurOutputBitmap?.let { blurOutputBitmap ->
input!!.destroy() input?.destroy()
input = null input = null
out!!.destroy() out?.destroy()
out = null out = null
blurIntrinsic!!.destroy() blurIntrinsic?.destroy()
blurIntrinsic = null blurIntrinsic = null
mBlurOut!!.destroy() mBlurOut?.destroy()
mBlurOut = null mBlurOut = null
yuvToRgbIntrinsic!!.destroy() yuvToRgbIntrinsic?.destroy()
yuvToRgbIntrinsic = null yuvToRgbIntrinsic = null
mBlurOutputBitmap!!.recycle() blurOutputBitmap.recycle()
mBlurOutputBitmap = null mBlurOutputBitmap = null
rs!!.destroy() rs?.destroy()
rs = null rs = null
} }
} }
private val cameraInstance: Camera internal class CompareSizesByArea : Comparator<Camera.Size> {
private get() { override fun compare(lhs: Camera.Size, rhs: Camera.Size): Int {
try { // We cast here to ensure the multiplications won't overflow
val currentCamera = 0 return java.lang.Long.signum(lhs.width.toLong() * lhs.height - rhs.width.toLong() * rhs.height)
mCamera = Camera.open(currentCamera) }
} catch (e: RuntimeException) { }
Log.e(TAG, "failed to open camera")
private fun chooseOptimalSize(
choices: List<Camera.Size>,
minWidth: Int,
minHeight: Int,
maxWidth: Int,
maxHeight: Int,
target: Size
): Camera.Size {
if (choices.isEmpty())
throw IllegalArgumentException()
// Collect the supported resolutions that are at least as big as the preview Surface
val bigEnough: MutableList<Camera.Size> = ArrayList()
// Collect the supported resolutions that are smaller than the preview Surface
val notBigEnough: MutableList<Camera.Size> = ArrayList()
val w = target.width
val h = target.height
for (option in choices) {
Log.w(TAG, "supportedSize: $option")
if (option.width <= maxWidth && option.height <= maxHeight && option.height == option.width * h / w) {
if (option.width >= minWidth && option.height >= minHeight) {
bigEnough.add(option)
} else {
notBigEnough.add(option)
}
}
}
// Pick the smallest of those big enough. If there is no one big enough, pick the
// largest of those not big enough.
return when {
bigEnough.size > 0 -> Collections.min(bigEnough, CompareSizesByArea())
notBigEnough.size > 0 -> Collections.max(notBigEnough, CompareSizesByArea())
else -> {
Log.e(TAG, "Couldn't find any suitable preview size")
choices[0]
}
} }
return mCamera!!
} }
private fun setUpCamera() { private fun setUpCamera() {
mDisposableBag.add(Single.fromCallable { cameraInstance } mDisposableBag.add(Single.fromCallable {
val currentCamera = 0
Camera.open(currentCamera)!!.apply { mCamera = this }
}
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ camera: Camera -> .subscribe({ camera: Camera ->
Log.w(TAG, "setUpCamera()") Log.w(TAG, "setUpCamera()")
val params = camera.parameters val params = camera.parameters
var selectSize: Camera.Size? = null val selectSize = chooseOptimalSize(params.supportedPictureSizes, 1280, 720, 1920, 1080, Size(1280, 720))
for (size in params.supportedPictureSizes) {
if (size.width == 1280 && size.height == 720) {
selectSize = size
break
}
}
checkNotNull(selectSize) { "No supported size" }
Log.w(TAG, "setUpCamera() selectSize " + selectSize.width + "x" + selectSize.height) Log.w(TAG, "setUpCamera() selectSize " + selectSize.width + "x" + selectSize.height)
params.setPictureSize(selectSize.width, selectSize.height) params.setPictureSize(selectSize.width, selectSize.height)
params.setPreviewSize(selectSize.width, selectSize.height) params.setPreviewSize(selectSize.width, selectSize.height)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment