Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Android Open Source Project
   *
   * 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 java.io.File;
The activity can crop specific region of interest from an image.
 
 public class CropImage extends MonitoredActivity {
     private static final String TAG = "CropImage";
 
     // These are various options can be specified in the intent.
     private Bitmap.CompressFormat mOutputFormat =
             ..// only used with mSaveUri
     private Uri mSaveUri = null;
     private boolean mSetWallpaper = false;
     private int mAspectXmAspectY;
     private boolean mDoFaceDetection = true;
     private boolean mCircleCrop = false;
     private final Handler mHandler = new Handler();
 
     // These options specifiy the output image size and whether we should
     // scale the output to fit it (or just crop it).
     private int mOutputXmOutputY;
     private boolean mScale;
     private boolean mScaleUp = true;
 
     boolean mWaitingToPick// Whether we are wait the user to pick a face.
     boolean mSaving;  // Whether the "save" button is already clicked.
 
     private CropImageView mImageView;
     private ContentResolver mContentResolver;
 
     private Bitmap mBitmap;
 
     private IImageList mAllImages;
     private IImage mImage;
 
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
          = getContentResolver();
 
         setContentView(..);
 
          = (CropImageViewfindViewById(..);
 
        MenuHelper.showStorageToast(this);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            if (extras.getString("circleCrop") != null) {
                 = true;
                 = 1;
                 = 1;
            }
             = (Uriextras.getParcelable(.);
            if ( != null) {
                String outputFormatString = extras.getString("outputFormat");
                if (outputFormatString != null) {
                     = Bitmap.CompressFormat.valueOf(
                            outputFormatString);
                }
            } else {
                 = extras.getBoolean("setWallpaper");
            }
             = (Bitmapextras.getParcelable("data");
             = extras.getInt("aspectX");
             = extras.getInt("aspectY");
             = extras.getInt("outputX");
             = extras.getInt("outputY");
             = extras.getBoolean("scale"true);
             = extras.getBoolean("scaleUpIfNeeded"true);
             = extras.containsKey("noFaceDetection")
                    ? !extras.getBoolean("noFaceDetection")
                    : true;
        }
        if ( == null) {
            Uri target = intent.getData();
             = ImageManager.makeImageList(target,
                    .);
             = .getImageForUri(target);
            if ( != null) {
                // Don't read in really large bitmaps. Use the (big) thumbnail
                // instead.
                // TODO when saving the resulting bitmap use the
                // decode/crop/encode api so we don't lose any resolution.
                 = .thumbBitmap(.);
            }
        }
        if ( == null) {
            finish();
            return;
        }
        // Make UI fullscreen.
                new View.OnClickListener() {
                    public void onClick(View v) {
                        setResult();
                        finish();
                    }
                });
                new View.OnClickListener() {
                    public void onClick(View v) {
                        onSaveClicked();
                    }
                });
        startFaceDetection();
    }
    private void startFaceDetection() {
        if (isFinishing()) {
            return;
        }
        Util.startBackgroundJob(thisnull,
                getResources().getString(..),
                new Runnable() {
            public void run() {
                final CountDownLatch latch = new CountDownLatch(1);
                final Bitmap b = ( != null)
                        ? .fullSizeBitmap(.,
                        1024 * 1024)
                        : ;
                .post(new Runnable() {
                    public void run() {
                        if (b !=  && b != null) {
                            .setImageBitmapResetBase(btrue);
                            .recycle();
                             = b;
                        }
                        if (.getScale() == 1F) {
                            .center(truetrue);
                        }
                        latch.countDown();
                    }
                });
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                .run();
            }
        }, );
    }
    private void onSaveClicked() {
        // TODO this code needs to change to use the decode/crop/encode single
        // step api so that we don't require that the whole (possibly large)
        // bitmap doesn't have to be read into memory
        if ( == null) {
            return;
        }
        if (return;
         = true;
        Bitmap croppedImage;
        // If the output is required to a specific size, create an new image
        // with the cropped image in the center and the extra space filled.
        if ( != 0 &&  != 0 && !) {
            // Don't scale the image but instead fill it so it's the
            // required dimension
            croppedImage = Bitmap.createBitmap(