Guest User

Untitled

a guest
Jun 2nd, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.List;
  3.  
  4. import android.content.Context;
  5. import android.hardware.Camera;
  6. import android.hardware.Camera.Size;
  7. import android.util.Log;
  8. import android.view.SurfaceHolder;
  9. import android.view.SurfaceHolder.Callback;
  10. import android.view.SurfaceView;
  11.  
  12. public class OverlayView extends SurfaceView implements Callback {
  13.  
  14. SurfaceHolder mHolder;
  15. Camera mCamera;
  16.  
  17. public OverlayView(Context context) {
  18. super(context);
  19.  
  20. // Install a SurfaceHolder.Callback so we get notified when the
  21. // underlying surface is created and destroyed.
  22. mHolder = getHolder();
  23. mHolder.addCallback(this);
  24. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  25.  
  26. }
  27.  
  28. public void surfaceCreated(SurfaceHolder holder) {
  29. // The Surface has been created, acquire the camera and tell it where
  30. // to draw.
  31. mCamera = Camera.open();
  32. mCamera.setDisplayOrientation(90);
  33. try {
  34. mCamera.setPreviewDisplay(holder);
  35. } catch (IOException e) {
  36. mCamera.release();
  37. mCamera = null;
  38. }
  39. }
  40.  
  41. public void surfaceDestroyed(SurfaceHolder holder) {
  42. // Surface will be destroyed when we return, so stop the preview.
  43. // Because the CameraDevice object is not a shared resource, it's very
  44. // important to release it when the activity is paused.
  45. mCamera.stopPreview();
  46. mCamera.release();
  47. mCamera = null;
  48. }
  49.  
  50. private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
  51. final double ASPECT_TOLERANCE = 0.1;
  52. double targetRatio = (double) w / h;
  53. if (sizes == null) return null;
  54.  
  55. Size optimalSize = null;
  56. double minDiff = Double.MAX_VALUE;
  57.  
  58. int targetHeight = h;
  59.  
  60. // Try to find an size match aspect ratio and size
  61. for (Size size : sizes) {
  62. double ratio = (double) size.width / size.height;
  63. if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
  64. if (Math.abs(size.height - targetHeight) < minDiff) {
  65. optimalSize = size;
  66. minDiff = Math.abs(size.height - targetHeight);
  67. }
  68. }
  69.  
  70. // Cannot find the one match the aspect ratio, ignore the requirement
  71. if (optimalSize == null) {
  72. minDiff = Double.MAX_VALUE;
  73. for (Size size : sizes) {
  74. if (Math.abs(size.height - targetHeight) < minDiff) {
  75. optimalSize = size;
  76. minDiff = Math.abs(size.height - targetHeight);
  77. }
  78. }
  79. }
  80. return optimalSize;
  81. }
  82.  
  83. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  84. // Now that the size is known, set up the camera parameters and begin
  85. // the preview.
  86. Camera.Parameters parameters = mCamera.getParameters();
  87.  
  88. List<Size> sizes = parameters.getSupportedPreviewSizes();
  89. Size optimalSize = getOptimalPreviewSize(sizes, w, h);
  90. Log.d("DEBUG", optimalSize.width + "," + optimalSize.height);
  91. parameters.setPreviewSize(optimalSize.width, optimalSize.height);
  92.  
  93. mCamera.setParameters(parameters);
  94. mCamera.startPreview();
  95.  
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment