Skip to content

Camera Kit

Antonio Zaitoun edited this page Jul 8, 2016 · 8 revisions

###Basic Initialization:

####Normal Camera:

import net.crofis.ui.camera.CameraActivity;
    CameraActivity.activity().start(this);

####Square Camera:

import net.crofis.ui.camera.SquareCameraActivity;
    SquareCameraActivity.activity().start(this);

###Advanced Initialization:

####- Using Activity Builder (Recommended) Using the static method activity() which exists in both classes: CameraActivity, SquareCameraActivity The method will return an object of type ActivityBuilder, note that there are two ActivityBuilder classes: CameraActivity.ActivityBuilder and SquareCameraActivity.ActivityBuilder which are both static and

ActivityBuilder public methods:

Note that SquareCameraActivity.ActivityBuilder class does not have the showZoomBar(boolean flag); method.

public static final class ActivityBuilder{
        /**
         * DISPLAY_FLASH_TOGGLE is true by default, however if you wish to disable the flash in the camera
         * (Hide the flash button) call this method with @param flag = false
         *
         * @param flag The flag that decides if the flash-toggle button will show in the camera activity.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder displayFlashToggle(boolean flag);

         /**
         * DISPLAY_SWITCH_CAM is true by default, however if you want to prevent the user from switching between their cameras
         * set @param flag to false
         *
         * @param flag The flag that decides if the cam-switch button will show in the camera activity.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder displayCameraSwitchToggle(boolean flag);

        /**
         * ALLOW_ZOOM_GESTURE is true by default. This flag , if enabled, will allow the user to pinch in order to zoom in and out.
         *
         * @param flag By setting this flag to false the user will lose capability of zooming in.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder allowZoomGesture(boolean flag);

        /**
         * SHOW_ZOOM_SEEKBAR is set to true by default - exclusive to CameraActivity only.
         * This will only have an impact if ALLOW_ZOOM_GESTURE is set to true.
         * When using the pinch gesture to zoom, if the flag is set to true, a Seek Bar will appear.
         * When dragging the "seek bar" the camera will zoom in and out according to the percentage.
         *
         * @param flag Set this to false if you don't want the seekbar to appear when using the pinch gesture.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder showZoomBar(boolean flag);

         /**
         * ALLOW_ROTATION_ANIMATION is true by default. This will have an effect only if ALLOW_ORIENTATION_CHANGE is also set to true.
         * If this flag was set to true, when rotating the device the buttons will rotate with a smooth rotation animation. To disable this animation
         * set this flag to false. Note that this won't prevent the icons from rotating, they will simply rotate without animation.
         *
         * @param flag Setting this to false will disable the animation that is applied to all views that rotate when the screen rotates.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder allowRotationAnimation(boolean flag);

        /**
         * SAVE_FILE_TO_STORAGE is set to false by default. When setting it to true any picture that is taken by the camera will be saved in a photo album.
         *
         * @param flag This allows you to save the images you capture into an external storage.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder saveFileToStorage(boolean flag);

         /**
         * SET_CROP_OPTIONAL is set to false by default. This option, when enabled, will present the user with an option to crop the image they captured
         * within the camera activity before sending it with the result intent.
         *
         * @param flag When set to true users will have the option to crop the image the captured.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder setCropOptional(boolean flag);

         /**
         * RETURN_DATA_AS_BYTE_ARRAY is true by default. Setting it to false will return the URI path of the image that was captured. This option may help
         * you in cases where the image is way too big in size to pass within a result intent.
         *
         * When enabled, the result intent will include the path in the keys "data" and "uri".
         *
         * @param flag By setting this to false, the result intent of the camera actvity will return the URI path of the image, resulting in a better performance.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder returnDataAsByteArray(boolean flag);

        /**
         * This Method is deprecated.
         * use setCropAspectRatio(int x,int y) instead.
         *
         * CROP_ASPECT_RATIO is set to -1 by default. This flag modification will only have an impact if SET_CROP_OPTIONAL is set to true.
         *
         * @param ratio is one of the following numbers: {0,1,2,3} where:
         *              0 = 1:1 ratio
         *              1 = 2:1 ratio
         *              2 = 4:3 ratio
         *              3 = 16:9 ratio
         * @return The ActivityBuilder that we are building
         */
        @Deprecated
        public ActivityBuilder setCropAspectRatio(int ratio);

        /**
         * CROP_ASPECT_RATIO is set to -1 by default. This flag modification will only have an impact if SET_CROP_OPTIONAL is set to true.
         *
         * Setting this flag will impact the CropView inside the camera activity, which will result with a locked aspect ratio of x:y
         * Note that x and y cannot be equal to or less than zero, However the application can handle an exception and simply will set no specified
         * aspect ratio and let the user choose a ratio.
         *
         * @param x The aspect ratio where x:y
         * @param y The aspect ratio where x:y
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder setCropAspectRatio(int x,int y);

        /**
         * ALLOW_ORIENTATION_CHANGE is set to true by default. By setting it to false, the gyro sensor will be locked
         * and the activity buttons will not rotate with the screen.
         *
         * @param flag Set this to false if you wish to disable UI rotations.
         * @return The ActivityBuilder that we are building
         */
        public ActivityBuilder setOrientationChangeEnabled(boolean flag);

        /**
         * LOAD_CAMERA flag is set to 0 by default, which is the back camera.
         * This is the camera that will be loaded when the activity starts.
         *
         * @param camId You can set this to either 1 (Face Cam) or 0 (Back Cam)
         * @return
         */
        public ActivityBuilder openWithCamera(int camId);

        /**
         * This method will start the camera activity with the flags that were set using other methods in this class.
         * Note this method uses an Activity Object to create and launh the intent.
         * If you cannot access an activity use the getIntent(Context context) to get the built intent.
         *
         * @param context is the current activity (application context).
         */
        public void start(Activity context);

        /**
         * This method should be used where you do not have an activity to pass to .start() method.
         *
         * @param context The Application context.
         * @return Will return the intent that was built using the parameters that were passed. Calling this method
         * directly will return an intent with the default settings.
         */
        public Intent getIntent(Context context);
}

####- Using Intent Builder: I recommend using the Activity Builder class however if you wish to create your own intent you can do so using these flags:

    public static final String FLAG_ALLOW_ORIENTATION_CHANGE = "ALLOW_ORIENTATION_CHANGE";
    public static final String FLAG_SET_CROP_ASPECT_RATIO = "CROP_ASPECT_RATIO";
    public static final String FLAG_SET_CROP_OPTIONAL = "SET_CROP_OPTIONAL";
    public static final String FLAG_DISPLAY_FLASH_TOGGLE = "DISPLAY_FLASH_TOGGLE";
    public static final String FLAG_DISPLAY_SWITCH_CAM = "DISPLAY_SWITCH_CAM";
    public static final String FLAG_SHOW_ZOOM_SEEKBAR = "SHOW_ZOOM_SEEKBAR";//Exclusive to CameraActivity
    public static final String FLAG_ALLOW_ZOOM_GESTURE = "ALLOW_ZOOM_GESTURE";
    public static final String FLAG_ALLOW_ROTATION_ANIMATION = "ALLOW_ROTATION_ANIMATION";
    public static final String FLAG_LOAD_CAM = "LOAD_CAMERA";
    public static final String FLAG_SAVE_TO_STORAGE = "SAVE_FILE_TO_STORAGE";
    public static final String FLAG_RETURN_DATA_AS_BYTE_ARRAY = "RETURN_DATA_AS_BYTE_ARRAY";

Note that you cannot use custom aspect ratio for cropping without the Activity Builder. however you can pass in one of these parameters:

    public static final int CROP_RATIO_NONE = -1;
    public static final int CROP_RATIO_1_1 = 0;
    public static final int CROP_RATIO_2_1 = 1;
    public static final int CROP_RATIO_4_3 = 2;
    public static final int CROP_RATIO_16_9 = 3;

#####Example Usage

     Intent intent = new Intent(context, SquareCameraActivity.class);
     intent.putExtra(SquareCameraActivity.FLAG_ALLOW_ORIENTATION_CHANGE,true);
     intent.putExtra(SquareCameraActivity.FLAG_SET_CROP_ASPECT_RATIO ,SquareCameraActivity.CROP_RATIO_1_1);
     intent.putExtra(SquareCameraActivity.FLAG_SET_CROP_OPTIONAL ,true);
     intent.putExtra(SquareCameraActivity.FLAG_DISPLAY_FLASH_TOGGLE ,true);
     intent.putExtra(SquareCameraActivity.FLAG_DISPLAY_SWITCH_CAM ,true);
     intent.putExtra(SquareCameraActivity.FLAG_ALLOW_ZOOM_GESTURE ,false);
     intent.putExtra(SquareCameraActivity.FLAG_ALLOW_ROTATION_ANIMATION ,true);
     intent.putExtra(SquareCameraActivity.FLAG_LOAD_CAM ,0);
     intent.putExtra(SquareCameraActivity.FLAG_SAVE_TO_STORAGE ,true);
     intent.putExtra(SquareCameraActivity.RETURN_DATA_AS_BYTE_ARRAY,true);
     startActivityForResult(intent, SquareCameraActivity.REQUEST_CODE);

###Get Result from Camera If you wish to get the path of the image you must set these flags:

    SAVE_FILE_TO_STORAGE      = true
    RETURN_DATA_AS_BYTE_ARRAY = false
  • In Activity Builder:
    CameraActivity.activity()
                  .saveFileToStorage(true)
                  .returnDataAsByteArray(false)
                  .start(this);
  • When creating an intent object:
    Intent intent = new Intent(this, SquareCameraActivity.class);
    intent.putExtra(SquareCameraActivity.FLAG_SAVE_TO_STORAGE,true);
    intent.putExtra(SquareCameraActivity.FLAG_RETURN_DATA_AS_BYTE_ARRAY, false);
    startActivityForResult(intent, SquareCameraActivity.REQUEST_CODE);

And then in the onActivityResult:

    String imageUriPath = resultIntent.getStringExtra("uri");

Or

    String imageUriPath = resultIntent.getStringExtra("data");

###onActivityResult Notice that getting the image as a byte array driectly from the result intent will have an impact of the image quality, since the bitmap is reduced in size to fit in the intent object. If you wish to get the full image take a look at the code above that shows you how to fetch the photo's URI path.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SquareCameraActivity.REQUEST_CODE:
                if (resultCode == 1) {
                    byte[] arr = data.getByteArrayExtra("data");
                    Bitmap bmp = BitmapFactory.decodeByteArray(arr, 0, arr.length);
                    ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bmp);
                }

                break;
            case CameraActivity.REQUEST_CODE:
                if (resultCode == 1) {
                    byte[] arr = data.getByteArrayExtra("data");
                    Bitmap bmp = BitmapFactory.decodeByteArray(arr, 0, arr.length);
                    ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bmp);
                }
                break;
        }
    }
Clone this wiki locally