Guest User

Untitled

a guest
Sep 5th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.28 KB | None | 0 0
  1. class CameraSurfaceHolder extends SurfaceHolder.Callback {
  2.   def surfaceCreated(holder: SurfaceHolder): Unit = {
  3.     CameraController.start()
  4.     Log.d(TAG, "--Surface created")
  5.   }
  6.  
  7.   def surfaceDestroyed(holder: SurfaceHolder): Unit = {
  8.     CameraController.stop()
  9.     Log.d(TAG, "--Surface destroyed")
  10.   }
  11.  
  12.   def surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int): Unit = {
  13.     CameraController.initPreview(holder, width, height)
  14.     Log.d(TAG, "--Surface changed")
  15.     //        CameraController.start()
  16.   }
  17. }
  18.  
  19. object CameraResolution extends Enumeration {
  20.   val Low, High = Value
  21. }
  22.  
  23. object CameraController {
  24.  
  25.   private var camera: Camera = _
  26.  
  27.   def start(): Unit = {
  28.  
  29.     if (camera == null) {
  30.       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
  31.         val info = new Camera.CameraInfo()
  32.         for (i <- 0 until Camera.getNumberOfCameras) {
  33.           Camera.getCameraInfo(i, info)
  34.           if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
  35.             try {
  36.               //            Log.d(TAG, "Camera %d is facing back".format(i))
  37.               camera = Camera.open(i)
  38.             } catch {
  39.               case e: Exception =>
  40.                 Log.d(TAG, "Cannot open camera " + i + " " + e.getMessage())
  41.             }
  42.           }
  43.         }
  44.       }
  45.     }
  46.     if (camera == null) {
  47.       camera = Camera.open()
  48.     }
  49.     camera.startPreview()
  50.   }
  51.  
  52.   def startPreview() = camera.startPreview()
  53.  
  54.   def stop(): Unit = {
  55.     camera.stopPreview()
  56.     camera.release()
  57.     camera = null
  58.   }
  59.  
  60.   def stopPreview() = camera.stopPreview()
  61.  
  62.   def takePicture(): Future[Array[Byte]] = {
  63.     val picture = Promise[Array[Byte]]
  64.     if (camera != null) {
  65.       try {
  66.         camera.autoFocus(new Camera.AutoFocusCallback() {
  67.           def onAutoFocus(success: Boolean, camera: Camera) {
  68.             if (success) {
  69.               camera.takePicture(null, null, new Camera.PictureCallback() {
  70.                 def onPictureTaken(data: Array[Byte], camera: Camera) {
  71.                   picture.success(data)
  72.                 }
  73.               })
  74.             } else {
  75.               picture.failure(new Exception("Cannot autofocus"))
  76.             }
  77.           }
  78.         })
  79.       } catch {
  80.         case e: Exception =>
  81.           picture.failure(e)
  82.       }
  83.     }
  84.     picture.future
  85.   }
  86.  
  87.   implicit class PrintableSize(size: Camera#Size) {
  88.     def value = "%d,%d".format(size.width, size.height)
  89.   }
  90.  
  91.   private[test] def initPreview(holder: SurfaceHolder, surfaceWidth: Int, surfaceHeight: Int): Unit = {
  92.  
  93.     if (camera != null && holder.getSurface != null) {
  94.         camera.setPreviewDisplay(holder)
  95.  
  96.       val (width, height) = if (surfaceWidth < surfaceHeight) (surfaceHeight, surfaceWidth) else (surfaceWidth, surfaceHeight)
  97.       Log.d(TAG, "Surface size: %d %d, aspect ratio: %g".format(width, height,1.0 * width / height))
  98.  
  99.       val parameters = camera.getParameters
  100.  
  101.       val aspect = 1.0 * width / height
  102.  
  103.       val previewSize = parameters.getSupportedPreviewSizes.reduce { (size1, size2) =>
  104.         val aspect1 = 1.0 * size1.width / size1.height
  105.         val aspect2 = 1.0 * size2.width / size2.height
  106.  
  107.         val score1 = 1000 * Math.abs(aspect1 - aspect) + Math.abs(surfaceWidth - size1.width)
  108.         val score2 = 1000 * Math.abs(aspect2 - aspect) + Math.abs(surfaceHeight - size2.width)
  109.  
  110.         if (score1 < score2) size1 else size2
  111.       }
  112.       parameters.setPreviewSize(previewSize.width, previewSize.height)
  113.       Log.d(TAG, "Selected preview Size: %d,%d, aspect: %g".format(previewSize.width, previewSize.height, 1.0 * previewSize.width / previewSize.height))
  114.  
  115.       parameters.setPictureFormat(ImageFormat.JPEG)
  116.       parameters.setFlashMode(camera.Parameters.FLASH_MODE_ON)
  117.       parameters.setFocusMode(camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)
  118.       parameters.setRotation(90)
  119.  
  120.       camera.setParameters(parameters)
  121.       camera.setDisplayOrientation(90)
  122.  
  123.       setResolution(CameraResolution.Low, aspect)
  124.     }
  125.   }
  126.  
  127.   def setResolution(resolution: CameraResolution.Value, aspect: Double = 1.5) {
  128.     //    stopPreview()
  129.     val parameters = camera.getParameters
  130.  
  131.       val pictureSize = parameters.getSupportedPictureSizes.reduce { (size1, size2) =>
  132.       val aspect1 = 1.0 * size1.width / size1.height
  133.       val aspect2 = 1.0 * size2.width / size2.height
  134.  
  135.       resolution match {
  136.         case CameraResolution.Low =>
  137.           val score1 = 1000 * Math.abs(aspect1 - aspect) + Math.abs(1000 - size1.width)
  138.           val score2 = 1000 * Math.abs(aspect2 - aspect) + Math.abs(1000 - size2.width)
  139.           if (score1 < score2) size1 else size2
  140.         case CameraResolution.High =>
  141.           val score1 = Math.abs(size1.width)
  142.           val score2 = Math.abs(size2.width)
  143.           if (score1 > score2) size1 else size2
  144.       }
  145.     }
  146.  
  147.     Log.d(TAG, "Setting resolution to: %d,%d, aspect: %g".format(pictureSize.width, pictureSize.height, 1.0 * pictureSize.width / pictureSize.height))
  148.     parameters.setPictureSize(pictureSize.width, pictureSize.height)
  149.  
  150.     val previewSize = parameters.getPreviewSize
  151.     Log.d(TAG, "Preview Size: %d,%d, aspect: %g".format(previewSize.width, previewSize.height, 1.0 * previewSize.width / previewSize.height))
  152.  
  153.     camera.setParameters(parameters)
  154.   }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment