Advertisement
Guest User

Untitled

a guest
May 25th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import android.content.Context;
  2. import android.hardware.Camera;
  3. import android.util.Log;
  4. import android.view.SurfaceHolder;
  5. import android.view.SurfaceView;
  6.  
  7. import java.io.IOException;
  8.  
  9.  
  10. public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
  11. private SurfaceHolder mHolder;
  12. private Camera mCamera;
  13.  
  14. public CameraPreview(Context context, Camera camera) {
  15. super(context);
  16. mCamera = camera;
  17.  
  18. // Install a SurfaceHolder.Callback so we get notified when the
  19. // underlying surface is created and destroyed.
  20. mHolder = getHolder();
  21. mHolder.addCallback(this);
  22.  
  23. // deprecated setting, but required on Android versions prior to 3.0
  24. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  25. }
  26.  
  27. public void surfaceCreated(SurfaceHolder holder) {
  28. // The Surface has been created, now tell the camera where to draw the preview.
  29. try {
  30. mCamera.setPreviewDisplay(holder);
  31. mCamera.startPreview();
  32. } catch (IOException e) {
  33. //Log.d(TAG, "Error setting camera preview: " + e.getMessage());
  34. }
  35. }
  36.  
  37. public void surfaceDestroyed(SurfaceHolder holder) {
  38. // empty. Take care of releasing the Camera preview in your activity.
  39. // stop preview before making changes
  40. try {
  41. mCamera.stopPreview();
  42. } catch (Exception e){
  43. // ignore: tried to stop a non-existent preview
  44. }
  45. }
  46.  
  47. /**
  48. * When this function returns, mCamera will be null.
  49. */
  50. public void stopPreview() {
  51.  
  52. if (mCamera != null) {
  53. mCamera.stopPreview();
  54. }
  55. }
  56.  
  57. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  58. // If your preview can change or rotate, take care of those events here.
  59. // Make sure to stop the preview before resizing or reformatting it.
  60.  
  61. if (mHolder.getSurface() == null){
  62. // preview surface does not exist
  63. return;
  64. }
  65.  
  66. // stop preview before making changes
  67. try {
  68. mCamera.stopPreview();
  69. } catch (Exception e){
  70. // ignore: tried to stop a non-existent preview
  71. }
  72.  
  73. // set preview size and make any resize, rotate or
  74. // reformatting changes here
  75.  
  76. // start preview with new settings
  77. try {
  78. mCamera.setPreviewDisplay(mHolder);
  79. mCamera.startPreview();
  80.  
  81. } catch (Exception e){
  82. //Log.d(TAG, "Error starting camera preview: " + e.getMessage());
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement