Android Open Source - android-cropimage Crop Image Intent Builder






From Project

Back to project page android-cropimage.

License

The source code is released under:

Apache License

If you think the Android project android-cropimage listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012 Lorenzo Villani.//w  ww  . ja va  2  s . co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

/**
 * By default the following features are enabled, unless you override them by calling setters in the
 * builder:
 *
 * <ul>
 * <li>Scale;</li>
 * <li>Scale up (if needed);</li>
 * <li>Face detection;</li>
 * </ul>
 *
 * @since 1.0.1
 */
// TODO: Circle Crop
// TODO: Set Wallpaper
public class CropImageIntentBuilder {
    private static final String EXTRA_ASPECT_X = "aspectX";
    private static final String EXTRA_ASPECT_Y = "aspectY";
    private static final String EXTRA_OUTPUT_X = "outputX";
    private static final String EXTRA_OUTPUT_Y = "outputY";
    private static final String EXTRA_BITMAP_DATA = "data";
    private static final String EXTRA_SCALE = "scale";
    private static final String EXTRA_SCALE_UP_IF_NEEDED = "scaleUpIfNeeded";
    private static final String EXTRA_NO_FACE_DETECTION = "noFaceDetection";
    private static final String EXTRA_CIRCLE_CROP = "circleCrop";
    private static final String EXTRA_OUTPUT_FORMAT = "outputFormat";
    private static final String EXTRA_OUTPUT_QUALITY = "outputQuality";

    private static final int DEFAULT_SCALE = 1;

    private boolean scale = true;
    private boolean scaleUpIfNeeded = true;
    private boolean doFaceDetection = true;
    private boolean circleCrop = false;
    private String outputFormat = null;
    private int outputQuality = 100;
    private Uri sourceImage;
    private Bitmap bitmap;

    private final int aspectX;
    private final int aspectY;
    private final int outputX;
    private final int outputY;
    private final Uri saveUri;

    /**
     * Constructor.
     *
     * @param outputX
     *        Output vertical size in pixels.
     * @param outputY
     *        Output horizontal size in pixels.
     * @param saveUri
     *        Output file URI.
     * @since 1.0.1
     */
    public CropImageIntentBuilder(final int outputX, final int outputY, final Uri saveUri) {
        this(DEFAULT_SCALE, DEFAULT_SCALE, outputX, outputY, saveUri);
    }

    /**
     * Constructor.
     *
     * @param aspectX
     *        Horizontal aspect ratio.
     * @param aspectY
     *        Vertical aspect ratio.
     * @param outputX
     *        Output vertical size in pixels.
     * @param outputY
     *        Output horizontal size in pixels.
     * @param saveUri
     *        Output file URI.
     * @since 1.0.1
     */
    public CropImageIntentBuilder(final int aspectX, final int aspectY, final int outputX,
            final int outputY, final Uri saveUri) {
        this.aspectX = aspectX;
        this.aspectY = aspectY;
        this.outputX = outputX;
        this.outputY = outputY;
        this.saveUri = saveUri;
    }

    /**
     * Builds the Intent.
     *
     * @param context
     *        The application context.
     * @return The newly created intent.
     * @since 1.0.1
     */
    public Intent getIntent(final Context context) {
        final Intent intent = new Intent(context, CropImage.class);

        //
        // Required Intent extras.
        //

        intent.putExtra(EXTRA_ASPECT_X, this.aspectX);
        intent.putExtra(EXTRA_ASPECT_Y, this.aspectY);
        intent.putExtra(EXTRA_OUTPUT_X, this.outputX);
        intent.putExtra(EXTRA_OUTPUT_Y, this.outputY);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, this.saveUri);

        //
        // Optional Intent Extras
        //

        intent.putExtra(EXTRA_SCALE, this.scale);
        intent.putExtra(EXTRA_SCALE_UP_IF_NEEDED, this.scaleUpIfNeeded);
        intent.putExtra(EXTRA_NO_FACE_DETECTION, !this.doFaceDetection);
        intent.putExtra(EXTRA_CIRCLE_CROP, this.circleCrop);
        intent.putExtra(EXTRA_OUTPUT_FORMAT, this.outputFormat);
        intent.putExtra(EXTRA_OUTPUT_QUALITY, this.outputQuality);

        if (this.bitmap != null) {
            intent.putExtra(EXTRA_BITMAP_DATA, this.bitmap);
        }

        if (this.sourceImage != null) {
            intent.setData(this.sourceImage);
        }

        return intent;
    }

    /**
     * Set the quality for the output file when applicable. This is used for JPEG output
     * format for example.
     *
     * @param outputQuality
     *        The quality (0 is smallest file size, 100 is best quality)
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setOutputQuality(final int outputQuality) {
        this.outputQuality = outputQuality;

        return this;
    }

    /**
     * Scales down the picture.
     *
     * @param scale
     *        Whether to scale down the image.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setScale(final boolean scale) {
        this.scale = scale;

        return this;
    }

    /**
     * Whether to scale up the image if the cropped region is smaller than the output size.
     *
     * @param scaleUpIfNeeded
     *        Whether to scale up the image.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setScaleUpIfNeeded(final boolean scaleUpIfNeeded) {
        this.scaleUpIfNeeded = scaleUpIfNeeded;

        return this;
    }

    /**
     * Performs face detection before allowing users to crop the image.
     *
     * @param doFaceDetection
     *        Whether to perform face detection.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setDoFaceDetection(final boolean doFaceDetection) {
        this.doFaceDetection = doFaceDetection;

        return this;
    }

    /**
     * Sets bitmap data to crop. Please note that this method overrides any source image set by
     * {@link #setSourceImage(Uri)}.
     *
     * @param bitmap
     *        The {@link Bitmap} to crop.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setBitmap(final Bitmap bitmap) {
        this.bitmap = bitmap;

        return this;
    }

    /**
     * Sets the Uri of the image to crop. It must be accessible to the calling application/activity.
     *
     * @param sourceImage
     *        Uri of the image to crop.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setSourceImage(final Uri sourceImage) {
        this.sourceImage = sourceImage;

        return this;
    }

    /**
     * Whether to crop the image as circle
     *
     * @param circleCrop
     *        Whether to crop the image as circle.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setCircleCrop(final boolean circleCrop) {
        this.circleCrop = circleCrop;

        return this;
    }

    /**
     * Set output format of the image to crop. If not set, JPEG will be used.
     *
     * @param outputFormat
     *        Output format of the image to crop, such as JPEG, PNG or WEBP.
     * @return This Builder object to allow for chaining of calls to set methods.
     * @since 1.0.1
     */
    public CropImageIntentBuilder setOutputFormat(final String outputFormat) {
        this.outputFormat = outputFormat;

        return this;
    }
}




Java Source Code List

com.android.camera.BitmapManager.java
com.android.camera.CropImageIntentBuilder.java
com.android.camera.CropImage.java
com.android.camera.HighlightView.java
com.android.camera.ImageManager.java
com.android.camera.ImageViewTouchBase.java
com.android.camera.MonitoredActivity.java
com.android.camera.NoSearchActivity.java
com.android.camera.RotateBitmap.java
com.android.camera.Util.java
com.android.camera.example.MainActivity.java
com.android.camera.example.MediaStoreUtils.java
com.android.camera.gallery.BaseImageList.java
com.android.camera.gallery.BaseImage.java
com.android.camera.gallery.IImageList.java
com.android.camera.gallery.IImage.java
com.android.camera.gallery.ImageListUber.java
com.android.camera.gallery.ImageList.java
com.android.camera.gallery.Image.java
com.android.camera.gallery.LruCache.java
com.android.camera.gallery.SingleImageList.java
com.android.camera.gallery.UriImage.java
com.android.camera.gallery.VideoList.java
com.android.camera.gallery.VideoObject.java