Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CameraSurfaceHolder extends SurfaceHolder.Callback {
- def surfaceCreated(holder: SurfaceHolder): Unit = {
- CameraController.start()
- Log.d(TAG, "--Surface created")
- }
- def surfaceDestroyed(holder: SurfaceHolder): Unit = {
- CameraController.stop()
- Log.d(TAG, "--Surface destroyed")
- }
- def surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int): Unit = {
- CameraController.initPreview(holder, width, height)
- Log.d(TAG, "--Surface changed")
- // CameraController.start()
- }
- }
- object CameraResolution extends Enumeration {
- val Low, High = Value
- }
- object CameraController {
- private var camera: Camera = _
- def start(): Unit = {
- if (camera == null) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
- val info = new Camera.CameraInfo()
- for (i <- 0 until Camera.getNumberOfCameras) {
- Camera.getCameraInfo(i, info)
- if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
- try {
- // Log.d(TAG, "Camera %d is facing back".format(i))
- camera = Camera.open(i)
- } catch {
- case e: Exception =>
- Log.d(TAG, "Cannot open camera " + i + " " + e.getMessage())
- }
- }
- }
- }
- }
- if (camera == null) {
- camera = Camera.open()
- }
- camera.startPreview()
- }
- def startPreview() = camera.startPreview()
- def stop(): Unit = {
- camera.stopPreview()
- camera.release()
- camera = null
- }
- def stopPreview() = camera.stopPreview()
- def takePicture(): Future[Array[Byte]] = {
- val picture = Promise[Array[Byte]]
- if (camera != null) {
- try {
- camera.autoFocus(new Camera.AutoFocusCallback() {
- def onAutoFocus(success: Boolean, camera: Camera) {
- if (success) {
- camera.takePicture(null, null, new Camera.PictureCallback() {
- def onPictureTaken(data: Array[Byte], camera: Camera) {
- picture.success(data)
- }
- })
- } else {
- picture.failure(new Exception("Cannot autofocus"))
- }
- }
- })
- } catch {
- case e: Exception =>
- picture.failure(e)
- }
- }
- picture.future
- }
- implicit class PrintableSize(size: Camera#Size) {
- def value = "%d,%d".format(size.width, size.height)
- }
- private[test] def initPreview(holder: SurfaceHolder, surfaceWidth: Int, surfaceHeight: Int): Unit = {
- if (camera != null && holder.getSurface != null) {
- camera.setPreviewDisplay(holder)
- val (width, height) = if (surfaceWidth < surfaceHeight) (surfaceHeight, surfaceWidth) else (surfaceWidth, surfaceHeight)
- Log.d(TAG, "Surface size: %d %d, aspect ratio: %g".format(width, height,1.0 * width / height))
- val parameters = camera.getParameters
- val aspect = 1.0 * width / height
- val previewSize = parameters.getSupportedPreviewSizes.reduce { (size1, size2) =>
- val aspect1 = 1.0 * size1.width / size1.height
- val aspect2 = 1.0 * size2.width / size2.height
- val score1 = 1000 * Math.abs(aspect1 - aspect) + Math.abs(surfaceWidth - size1.width)
- val score2 = 1000 * Math.abs(aspect2 - aspect) + Math.abs(surfaceHeight - size2.width)
- if (score1 < score2) size1 else size2
- }
- parameters.setPreviewSize(previewSize.width, previewSize.height)
- Log.d(TAG, "Selected preview Size: %d,%d, aspect: %g".format(previewSize.width, previewSize.height, 1.0 * previewSize.width / previewSize.height))
- parameters.setPictureFormat(ImageFormat.JPEG)
- parameters.setFlashMode(camera.Parameters.FLASH_MODE_ON)
- parameters.setFocusMode(camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)
- parameters.setRotation(90)
- camera.setParameters(parameters)
- camera.setDisplayOrientation(90)
- setResolution(CameraResolution.Low, aspect)
- }
- }
- def setResolution(resolution: CameraResolution.Value, aspect: Double = 1.5) {
- // stopPreview()
- val parameters = camera.getParameters
- val pictureSize = parameters.getSupportedPictureSizes.reduce { (size1, size2) =>
- val aspect1 = 1.0 * size1.width / size1.height
- val aspect2 = 1.0 * size2.width / size2.height
- resolution match {
- case CameraResolution.Low =>
- val score1 = 1000 * Math.abs(aspect1 - aspect) + Math.abs(1000 - size1.width)
- val score2 = 1000 * Math.abs(aspect2 - aspect) + Math.abs(1000 - size2.width)
- if (score1 < score2) size1 else size2
- case CameraResolution.High =>
- val score1 = Math.abs(size1.width)
- val score2 = Math.abs(size2.width)
- if (score1 > score2) size1 else size2
- }
- }
- Log.d(TAG, "Setting resolution to: %d,%d, aspect: %g".format(pictureSize.width, pictureSize.height, 1.0 * pictureSize.width / pictureSize.height))
- parameters.setPictureSize(pictureSize.width, pictureSize.height)
- val previewSize = parameters.getPreviewSize
- Log.d(TAG, "Preview Size: %d,%d, aspect: %g".format(previewSize.width, previewSize.height, 1.0 * previewSize.width / previewSize.height))
- camera.setParameters(parameters)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment