Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CameraPreview {
- private static final String TAG = CameraPreview.class.getName();
- private static final int REQUEST_CAMERA_PERMISSION = 1;
- private static final String FRAGMENT_DIALOG = "dialog";
- private static final int[] FLASH_OPTIONS = {
- com.google.android.cameraview.CameraView.FLASH_AUTO,
- com.google.android.cameraview.CameraView.FLASH_OFF,
- com.google.android.cameraview.CameraView.FLASH_ON,
- };
- private static final int[] FLASH_ICONS = {
- R.drawable.ic_flash_auto_black_24dp,
- R.drawable.ic_flash_off_black_24dp,
- R.drawable.ic_flash_on_black_24dp,
- };
- private static final String[] FLASH_TITLES = {
- "Flash Auto",
- "Flash Off",
- "Flash On",
- };
- private Activity mActivity;
- private int mCurrentFlash;
- private Handler mBackgroundHandler;
- private CameraView mCameraView;
- private FragmentManager mFragmentManager;
- private CameraPreviewListener cameraPreviewListener;
- private int facing = CameraView.FACING_BACK;
- public CameraPreview(Activity activity, CameraView cameraView, FragmentManager supportFragmentManager) {
- this.mActivity = activity;
- this.mCameraView = cameraView;
- this.mFragmentManager = supportFragmentManager;
- build();
- }
- private void build() {
- if (mCameraView != null) {
- mCameraView.addCallback(mCallback);
- //mCameraView.setFacing(facing);
- mCameraView.setFlash(CameraView.FLASH_OFF);
- }
- }
- public void checkPermission() {
- if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA)
- == PackageManager.PERMISSION_GRANTED &&
- ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
- == PackageManager.PERMISSION_GRANTED) {
- mCameraView.start();
- } else if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
- Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
- Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
- ConfirmationDialogFragment
- .newInstance(R.string.permission_camera,
- new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
- REQUEST_CAMERA_PERMISSION,
- R.string.camera_permission_not_granted)
- .show(mFragmentManager, FRAGMENT_DIALOG);
- } else {
- ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA,
- Manifest.permission.WRITE_EXTERNAL_STORAGE},
- REQUEST_CAMERA_PERMISSION);
- }
- }
- public void stopCamera() {
- mCameraView.stop();
- }
- public void destroy() {
- if (mBackgroundHandler != null) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
- mBackgroundHandler.getLooper().quitSafely();
- } else {
- mBackgroundHandler.getLooper().quit();
- }
- mBackgroundHandler = null;
- }
- }
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
- @NonNull int[] grantResults) {
- switch (requestCode) {
- case REQUEST_CAMERA_PERMISSION:
- if (permissions.length != 2 || grantResults.length != 2) {
- throw new RuntimeException("Error on requesting camera permission.");
- }
- if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(mActivity, R.string.camera_permission_not_granted,
- Toast.LENGTH_SHORT).show();
- }
- // No need to start camera here; it is handled by onResume
- break;
- }
- }
- private Handler getBackgroundHandler() {
- if (mBackgroundHandler == null) {
- HandlerThread thread = new HandlerThread("background");
- thread.start();
- mBackgroundHandler = new Handler(thread.getLooper());
- }
- return mBackgroundHandler;
- }
- private CameraView.Callback mCallback
- = new CameraView.Callback() {
- @Override
- public void onCameraOpened(CameraView cameraView) {
- }
- @Override
- public void onCameraClosed(CameraView cameraView) {
- }
- @Override
- public void onPictureTaken(CameraView cameraView, final byte[] data) {
- final File outputPic = getOutputPictureFile();
- ImageUtil.saveToDiskAsync(
- data,
- outputPic,
- e -> {
- if (e == null) {
- cameraPreviewListener.onPictureTaken(outputPic);
- } else {
- ToastFactory.showToast(mActivity, "Cannot write image. " + e.getMessage());
- }
- });
- }
- };
- public void takePicture() {
- if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA)
- == PackageManager.PERMISSION_GRANTED &&
- ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
- == PackageManager.PERMISSION_GRANTED) {
- mCameraView.takePicture();
- } else if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
- Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
- Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
- ConfirmationDialogFragment
- .newInstance(R.string.permission_camera,
- new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
- REQUEST_CAMERA_PERMISSION,
- R.string.camera_permission_not_granted)
- .show(mFragmentManager, FRAGMENT_DIALOG);
- } else {
- ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA,
- Manifest.permission.WRITE_EXTERNAL_STORAGE},
- REQUEST_CAMERA_PERMISSION);
- }
- }
- public void switchCamera(ImageView view, int properties, int drawable) {
- view.setImageDrawable(ContextCompat.getDrawable(mActivity, drawable));
- mCameraView.setFacing(properties);
- }
- public void changePropFlash(ImageView view, int properties, int drawable) {
- view.setImageDrawable(ContextCompat.getDrawable(mActivity, drawable));
- mCameraView.setFlash(properties);
- }
- public void setCameraPreviewListener(CameraPreviewListener cameraPreviewListener) {
- this.cameraPreviewListener = cameraPreviewListener;
- }
- public static class ConfirmationDialogFragment extends DialogFragment {
- private static final String ARG_MESSAGE = "message";
- private static final String ARG_PERMISSIONS = "permissions";
- private static final String ARG_REQUEST_CODE = "request_code";
- private static final String ARG_NOT_GRANTED_MESSAGE = "not_granted_message";
- public static ConfirmationDialogFragment newInstance(@StringRes int message,
- String[] permissions, int requestCode, @StringRes int notGrantedMessage) {
- ConfirmationDialogFragment fragment = new ConfirmationDialogFragment();
- Bundle args = new Bundle();
- args.putInt(ARG_MESSAGE, message);
- args.putStringArray(ARG_PERMISSIONS, permissions);
- args.putInt(ARG_REQUEST_CODE, requestCode);
- args.putInt(ARG_NOT_GRANTED_MESSAGE, notGrantedMessage);
- fragment.setArguments(args);
- return fragment;
- }
- @NonNull
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- final Bundle args = getArguments();
- return new AlertDialog.Builder(getActivity())
- .setMessage(args.getInt(ARG_MESSAGE))
- .setPositiveButton(android.R.string.ok,
- (dialog, which) -> {
- String[] permissions = args.getStringArray(ARG_PERMISSIONS);
- if (permissions == null) {
- throw new IllegalArgumentException();
- }
- ActivityCompat.requestPermissions(getActivity(),
- permissions, args.getInt(ARG_REQUEST_CODE));
- })
- .setNegativeButton(android.R.string.cancel,
- (dialog, which) -> Toast.makeText(getActivity(),
- args.getInt(ARG_NOT_GRANTED_MESSAGE),
- Toast.LENGTH_SHORT).show())
- .create();
- }
- }
- protected final File getOutputPictureFile() {
- return makeTempFile(
- mActivity, mActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath(), "IMG_", ".jpg");
- }
- public static File makeTempFile(
- @NonNull Context context, @Nullable String saveDir, String prefix, String extension) {
- if (saveDir == null) saveDir = context.getExternalCacheDir().getAbsolutePath();
- final String timeStamp =
- new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
- final File dir = new File(saveDir);
- dir.mkdirs();
- return new File(dir, prefix + timeStamp + extension);
- }
- public interface CameraPreviewListener {
- void onPictureTaken(File outputFile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement