Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1.  
  2. public class CameraPreview {
  3.  
  4. private static final String TAG = CameraPreview.class.getName();
  5.  
  6. private static final int REQUEST_CAMERA_PERMISSION = 1;
  7.  
  8. private static final String FRAGMENT_DIALOG = "dialog";
  9.  
  10. private static final int[] FLASH_OPTIONS = {
  11. com.google.android.cameraview.CameraView.FLASH_AUTO,
  12. com.google.android.cameraview.CameraView.FLASH_OFF,
  13. com.google.android.cameraview.CameraView.FLASH_ON,
  14. };
  15.  
  16. private static final int[] FLASH_ICONS = {
  17. R.drawable.ic_flash_auto_black_24dp,
  18. R.drawable.ic_flash_off_black_24dp,
  19. R.drawable.ic_flash_on_black_24dp,
  20. };
  21.  
  22. private static final String[] FLASH_TITLES = {
  23. "Flash Auto",
  24. "Flash Off",
  25. "Flash On",
  26. };
  27.  
  28. private Activity mActivity;
  29. private int mCurrentFlash;
  30. private Handler mBackgroundHandler;
  31. private CameraView mCameraView;
  32. private FragmentManager mFragmentManager;
  33. private CameraPreviewListener cameraPreviewListener;
  34. private int facing = CameraView.FACING_BACK;
  35.  
  36. public CameraPreview(Activity activity, CameraView cameraView, FragmentManager supportFragmentManager) {
  37. this.mActivity = activity;
  38. this.mCameraView = cameraView;
  39. this.mFragmentManager = supportFragmentManager;
  40. build();
  41. }
  42.  
  43. private void build() {
  44. if (mCameraView != null) {
  45. mCameraView.addCallback(mCallback);
  46. //mCameraView.setFacing(facing);
  47. mCameraView.setFlash(CameraView.FLASH_OFF);
  48. }
  49. }
  50.  
  51. public void checkPermission() {
  52. if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA)
  53. == PackageManager.PERMISSION_GRANTED &&
  54. ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
  55. == PackageManager.PERMISSION_GRANTED) {
  56. mCameraView.start();
  57. } else if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
  58. Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
  59. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  60. ConfirmationDialogFragment
  61. .newInstance(R.string.permission_camera,
  62. new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
  63. REQUEST_CAMERA_PERMISSION,
  64. R.string.camera_permission_not_granted)
  65. .show(mFragmentManager, FRAGMENT_DIALOG);
  66. } else {
  67. ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA,
  68. Manifest.permission.WRITE_EXTERNAL_STORAGE},
  69. REQUEST_CAMERA_PERMISSION);
  70. }
  71. }
  72.  
  73. public void stopCamera() {
  74. mCameraView.stop();
  75. }
  76.  
  77. public void destroy() {
  78. if (mBackgroundHandler != null) {
  79. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  80. mBackgroundHandler.getLooper().quitSafely();
  81. } else {
  82. mBackgroundHandler.getLooper().quit();
  83. }
  84. mBackgroundHandler = null;
  85. }
  86. }
  87.  
  88. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  89. @NonNull int[] grantResults) {
  90. switch (requestCode) {
  91. case REQUEST_CAMERA_PERMISSION:
  92. if (permissions.length != 2 || grantResults.length != 2) {
  93. throw new RuntimeException("Error on requesting camera permission.");
  94. }
  95. if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
  96. Toast.makeText(mActivity, R.string.camera_permission_not_granted,
  97. Toast.LENGTH_SHORT).show();
  98. }
  99. // No need to start camera here; it is handled by onResume
  100. break;
  101. }
  102. }
  103.  
  104. private Handler getBackgroundHandler() {
  105. if (mBackgroundHandler == null) {
  106. HandlerThread thread = new HandlerThread("background");
  107. thread.start();
  108. mBackgroundHandler = new Handler(thread.getLooper());
  109. }
  110. return mBackgroundHandler;
  111. }
  112.  
  113. private CameraView.Callback mCallback
  114. = new CameraView.Callback() {
  115.  
  116. @Override
  117. public void onCameraOpened(CameraView cameraView) {
  118.  
  119. }
  120.  
  121. @Override
  122. public void onCameraClosed(CameraView cameraView) {
  123.  
  124. }
  125.  
  126. @Override
  127. public void onPictureTaken(CameraView cameraView, final byte[] data) {
  128. final File outputPic = getOutputPictureFile();
  129. ImageUtil.saveToDiskAsync(
  130. data,
  131. outputPic,
  132. e -> {
  133. if (e == null) {
  134. cameraPreviewListener.onPictureTaken(outputPic);
  135. } else {
  136. ToastFactory.showToast(mActivity, "Cannot write image. " + e.getMessage());
  137. }
  138. });
  139. }
  140. };
  141.  
  142. public void takePicture() {
  143. if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA)
  144. == PackageManager.PERMISSION_GRANTED &&
  145. ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
  146. == PackageManager.PERMISSION_GRANTED) {
  147. mCameraView.takePicture();
  148. } else if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
  149. Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
  150. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
  151. ConfirmationDialogFragment
  152. .newInstance(R.string.permission_camera,
  153. new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
  154. REQUEST_CAMERA_PERMISSION,
  155. R.string.camera_permission_not_granted)
  156. .show(mFragmentManager, FRAGMENT_DIALOG);
  157. } else {
  158. ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA,
  159. Manifest.permission.WRITE_EXTERNAL_STORAGE},
  160. REQUEST_CAMERA_PERMISSION);
  161. }
  162. }
  163.  
  164. public void switchCamera(ImageView view, int properties, int drawable) {
  165. view.setImageDrawable(ContextCompat.getDrawable(mActivity, drawable));
  166. mCameraView.setFacing(properties);
  167. }
  168.  
  169. public void changePropFlash(ImageView view, int properties, int drawable) {
  170. view.setImageDrawable(ContextCompat.getDrawable(mActivity, drawable));
  171. mCameraView.setFlash(properties);
  172. }
  173.  
  174. public void setCameraPreviewListener(CameraPreviewListener cameraPreviewListener) {
  175. this.cameraPreviewListener = cameraPreviewListener;
  176. }
  177.  
  178. public static class ConfirmationDialogFragment extends DialogFragment {
  179.  
  180. private static final String ARG_MESSAGE = "message";
  181. private static final String ARG_PERMISSIONS = "permissions";
  182. private static final String ARG_REQUEST_CODE = "request_code";
  183. private static final String ARG_NOT_GRANTED_MESSAGE = "not_granted_message";
  184.  
  185. public static ConfirmationDialogFragment newInstance(@StringRes int message,
  186. String[] permissions, int requestCode, @StringRes int notGrantedMessage) {
  187. ConfirmationDialogFragment fragment = new ConfirmationDialogFragment();
  188. Bundle args = new Bundle();
  189. args.putInt(ARG_MESSAGE, message);
  190. args.putStringArray(ARG_PERMISSIONS, permissions);
  191. args.putInt(ARG_REQUEST_CODE, requestCode);
  192. args.putInt(ARG_NOT_GRANTED_MESSAGE, notGrantedMessage);
  193. fragment.setArguments(args);
  194. return fragment;
  195. }
  196.  
  197. @NonNull
  198. @Override
  199. public Dialog onCreateDialog(Bundle savedInstanceState) {
  200. final Bundle args = getArguments();
  201. return new AlertDialog.Builder(getActivity())
  202. .setMessage(args.getInt(ARG_MESSAGE))
  203. .setPositiveButton(android.R.string.ok,
  204. (dialog, which) -> {
  205. String[] permissions = args.getStringArray(ARG_PERMISSIONS);
  206. if (permissions == null) {
  207. throw new IllegalArgumentException();
  208. }
  209. ActivityCompat.requestPermissions(getActivity(),
  210. permissions, args.getInt(ARG_REQUEST_CODE));
  211. })
  212. .setNegativeButton(android.R.string.cancel,
  213. (dialog, which) -> Toast.makeText(getActivity(),
  214. args.getInt(ARG_NOT_GRANTED_MESSAGE),
  215. Toast.LENGTH_SHORT).show())
  216. .create();
  217. }
  218.  
  219. }
  220.  
  221. protected final File getOutputPictureFile() {
  222. return makeTempFile(
  223. mActivity, mActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath(), "IMG_", ".jpg");
  224. }
  225.  
  226. public static File makeTempFile(
  227. @NonNull Context context, @Nullable String saveDir, String prefix, String extension) {
  228. if (saveDir == null) saveDir = context.getExternalCacheDir().getAbsolutePath();
  229. final String timeStamp =
  230. new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
  231. final File dir = new File(saveDir);
  232. dir.mkdirs();
  233. return new File(dir, prefix + timeStamp + extension);
  234. }
  235.  
  236. public interface CameraPreviewListener {
  237. void onPictureTaken(File outputFile);
  238. }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement