Advertisement
Guest User

Untitled

a guest
May 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import android.Manifest;
  2. import android.content.pm.PackageManager;
  3. import android.graphics.SurfaceTexture;
  4. import android.hardware.Camera;
  5. import android.support.v4.app.ActivityCompat;
  6. import android.support.v4.content.ContextCompat;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.Surface;
  10. import android.view.TextureView;
  11.  
  12. import java.io.IOException;
  13. import java.util.HashMap;
  14.  
  15. public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, ActivityCompat.OnRequestPermissionsResultCallback {
  16.  
  17. private Camera mCamera;
  18. private TextureView mTextureView;
  19. private static final int REQUEST_CAMERA_PERMISSION = 1;
  20. private static HashMap<Integer, Integer> ORIENTATIONS = new HashMap<Integer, Integer>();
  21. static {
  22. ORIENTATIONS.put(Surface.ROTATION_90, 0);
  23. ORIENTATIONS.put(Surface.ROTATION_0, 90);
  24. ORIENTATIONS.put(Surface.ROTATION_270, 180);
  25. ORIENTATIONS.put(Surface.ROTATION_180, 270);
  26. }
  27.  
  28. @Override
  29. protected void onResume() {
  30. super.onResume();
  31.  
  32. if (mTextureView.isAvailable()) {
  33. openCamera(mTextureView.getSurfaceTexture());
  34. } else {
  35. mTextureView.setSurfaceTextureListener(this);
  36. }
  37. }
  38.  
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. mTextureView = new TextureView(this);
  42. mTextureView.setSurfaceTextureListener(this);
  43. setContentView(mTextureView);
  44. }
  45.  
  46. private void openCamera(SurfaceTexture surface) {
  47. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
  48. != PackageManager.PERMISSION_GRANTED) {
  49. requestCameraPermission();
  50. return;
  51. }
  52.  
  53. mCamera = Camera.open();
  54.  
  55. try {
  56. mCamera.setPreviewTexture(surface);
  57. int rotation = getWindowManager().getDefaultDisplay()
  58. .getRotation();
  59. mCamera.setDisplayOrientation(ORIENTATIONS.get(rotation));
  60. Camera.Parameters params = mCamera.getParameters();
  61. params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
  62. mCamera.setParameters(params);
  63. mCamera.startPreview();
  64. } catch (IOException ioe) {
  65. }
  66. }
  67. public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
  68. openCamera(surface);
  69. }
  70.  
  71. public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  72. }
  73.  
  74. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  75. if (mCamera != null) {
  76. mCamera.stopPreview();
  77. mCamera.release();
  78. }
  79.  
  80. return true;
  81. }
  82.  
  83. public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  84. }
  85.  
  86. private void requestCameraPermission() {
  87. requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
  88. }
  89.  
  90. @Override
  91. public void onRequestPermissionsResult(int requestCode,
  92. String[] permissions, int[] grantResults) {
  93. switch (requestCode) {
  94. case REQUEST_CAMERA_PERMISSION: {
  95. if (grantResults.length > 0
  96. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  97.  
  98. } else {
  99.  
  100. }
  101. return;
  102. }
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement