Beispiel #1
0
 private void enableCropping() {
   mZooming = false;
   mResultImageView.setOnTouchListener(null);
   getActionBar().setSubtitle(R.string.subtitle_activity_cropping);
   getActionBar().setTitle(R.string.title_activity_cropping);
   if (mCropRectangle != null) {
     mCropRectangle.setReportView(mResultImageView, this);
     mCropRectangle.enable();
     mImageResultContainer.addView(mCropRectangle);
   }
   invalidateOptionsMenu();
 }
Beispiel #2
0
 private void enableZooming() {
   Log.d(TAG, "enable zoom ");
   mZooming = true;
   getActionBar().setSubtitle(R.string.subtitle_activity_zooming);
   getActionBar().setTitle(R.string.title_activity_zooming);
   if (mCropRectangle != null && mCropRectangle.getParent() != null) {
     mCropRectangle.disable();
     if (mCropRectangle.getParent() == null) {
       Log.d(Constants.DEBUG, "crop rectangle parent is null, not removed!");
     } else {
       ((RelativeLayout) mCropRectangle.getParent()).removeView(mCropRectangle);
     }
   }
   mResultImageView.setOnTouchListener(new OnZoomTouchListener());
   invalidateOptionsMenu();
 }
Beispiel #3
0
  // Some lifecycle callbacks so that the image can survive orientation change
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    Log.d(Constants.DEBUG, "onsaveinstance imgres " + mImageResized);

    Log.d(Constants.DEBUG, "onsaveinstance mimageheight " + mImageHeight);
    outState.putString(Constants.PHOTO_PATH_KEY, mCurrentPhotoPath);
    outState.putInt(IMAGE_HEIGHT, mImageHeight);
    outState.putInt(IMAGE_WIDTH, mImageWidth);
    if (mCropRectangle != null) {
      RectF r = mCropRectangle.getSelectedRect();
      outState.putInt(RECT_LEFT, (int) r.left);
      outState.putInt(RECT_TOP, (int) r.top);
      outState.putInt(RECT_RIGHT, (int) r.right);
      outState.putInt(RECT_BOTTOM, (int) r.bottom);
    }

    super.onSaveInstanceState(outState);
  }
Beispiel #4
0
  public void crop() {
    // TODO resize to max w/h
    ImageView resultImageView = (ImageView) findViewById(R.id.resultImageView);

    // mResultImageView.buildDrawingCache();
    // Bitmap mBitmap = mResultImageView.getDrawingCache();
    //
    //
    // BitmapDrawable drawable = (BitmapDrawable)
    // imageview.getDrawable();
    // Bitmap mBitmap = drawable.getBitmap();
    Bitmap croppedImage;

    int imageViewHeight = resultImageView.getMeasuredHeight();

    scaleRatio = ((float) imageViewHeight) / mImageHeight;

    Log.d(Constants.DEBUG, "CROP scaleratio: " + scaleRatio);

    Log.d(
        Constants.DEBUG,
        "CROP rectangle left: "
            + mCropRectangle.getSelectedRect().left
            + " top: "
            + mCropRectangle.getSelectedRect().top
            + " right: "
            + mCropRectangle.getSelectedRect().right
            + " bottom: "
            + mCropRectangle.getSelectedRect().bottom);

    Log.d(
        Constants.DEBUG,
        "CROP x: "
            + (int) (mCropRectangle.getSelectedX() / scaleRatio)
            + " y: "
            + (int) (mCropRectangle.getSelectedY() / scaleRatio)
            + " h: "
            + (scaleRatio * mCropRectangle.getHeight())
            + " w: "
            + (scaleRatio * mCropRectangle.getWidth()));
    // int croppedWidth = (int) (mCropRectangle.getSelectedWidth() /
    // scaleRatio);
    // int croppedHeight = (int) (mCropRectangle.getSelectedHeight() /
    // scaleRatio);
    //

    Matrix inverse = new Matrix();
    mMatrix.invert(inverse);

    // map touch point from ImageView to image
    float[] leftPoint = new float[] {mCropRectangle.getSelectedX(), mCropRectangle.getSelectedY()};
    inverse.mapPoints(leftPoint);
    float[] rightPoint =
        new float[] {mCropRectangle.getSelectedX2(), mCropRectangle.getSelectedY2()};
    inverse.mapPoints(rightPoint);

    int croppedWidth = (int) Math.abs(leftPoint[0] - rightPoint[0]);
    int croppedHeight = (int) Math.abs(leftPoint[1] - rightPoint[1]);

    Log.d(Constants.DEBUG, "CROP width: " + croppedWidth + " height: " + croppedHeight);

    croppedImage =
        cropBitmap(
            mCurrentPhotoPath, (int) leftPoint[0], (int) leftPoint[1], croppedWidth, croppedHeight);
    // croppedImage = cropBitmap(mCurrentPhotoPath, (int)
    // (mCropRectangle.getSelectedX() / scaleRatio),
    // (int) (mCropRectangle.getSelectedY() / scaleRatio), croppedWidth,
    // croppedHeight);
    if (croppedImage != null) {
      // restriction: height*width < 200,000 pixel && size < 50,000 bytes

      Log.d(Constants.DEBUG, "croppedH * W > 200,000? " + croppedHeight * croppedWidth);
      Log.d(
          Constants.DEBUG,
          "Utils.getSizeOf(croppedImage) > 50000? " + Utils.getSizeOf(croppedImage));
      if (croppedHeight * croppedWidth > 200000 /*
													 * ||
													 * Utils.getSizeOf(croppedImage
													 * ) > 600000
													 */) {
        // if (croppedHeight > Constants.MAX_IMAGE_HEIGHT_LINE) {
        // TODO resizing for vertical images
        int w = (int) (((float) Constants.MAX_IMAGE_HEIGHT_LINE / croppedHeight) * croppedWidth);
        Log.d(Constants.DEBUG, "cropped w " + croppedWidth);
        Log.d(Constants.DEBUG, "scaled cropped w " + w);
        croppedImage =
            Bitmap.createScaledBitmap(croppedImage, w, Constants.MAX_IMAGE_HEIGHT_LINE, false);
        croppedHeight = Constants.MAX_IMAGE_HEIGHT_LINE;
        croppedWidth = w;
      }

      // TODO: if possible to save cropped images, file names need to be
      // unique
      mCurrentCroppedPath = mCurrentPhotoPath.replace(".jpg", "c.jpg");
      overwriteImage(croppedImage, mCurrentCroppedPath);
      croppedImage.recycle();
      croppedImage = null;

      Intent intent = new Intent(this, RecognitionActivity.class);
      intent.putExtra(Constants.PHOTO_PATH_KEY, mCurrentCroppedPath);
      intent.putExtra(Constants.CAMERA_PICTURE_HEIGHT, croppedHeight);
      intent.putExtra(Constants.CAMERA_PICTURE_WIDTH, croppedWidth);
      intent.putExtra(Constants.USE_LINE_RECOGNITION, true);
      intent.putExtra(Constants.KANJI_LITERAL, mFindString);
      intent.putExtra(
          Constants.RECOGNITION_ACTIVITY_MODE,
          mMode == R.id.ID_MODE_SCENERY ? R.id.ID_MODE_LINE : mMode);
      intent.putExtra(Constants.FIND_MODE, mFindMode);
      intent.putExtra(Constants.LIST_ID, mListId);
      intent.putExtra(Constants.KANJI_ID, mKanjiId);
      intent.putExtra(Constants.SAVED_KANJI, mSavedKanji);
      startActivity(intent);
      finish();
    }
  }
Beispiel #5
0
  private void setUI() {
    getActionBar().setSubtitle(R.string.subtitle_activity_cropping);

    mImageResultContainer = (RelativeLayout) findViewById(R.id.imageResultContainer);
    LayoutInflater inflater = getLayoutInflater();
    inflater.inflate(R.layout.crop_rectangle, mImageResultContainer, true);
    // mImageResultContainer.addView(mCropRectangle);
    mCropRectangle = (CropRectangle) findViewById(R.id.cropRectangle);

    ((RelativeLayout) mCropRectangle.getParent()).removeView(mCropRectangle);
    mImageResultContainer.addView(mCropRectangle);

    // Fetch screen height and width, to use as our max size when loading
    // images as this
    // activity runs full screen
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    // For this sample we'll use half of the longest width to resize our
    // images. As the
    // image scaling ensures the image is larger than this, we should be
    // left with a
    // resolution that is appropriate for both portrait and landscape.
    // For
    // best image quality
    // we shouldn't divide by 2, but this will use more memory and
    // require a
    // larger memory
    // cache.
    final int longest = (height > width ? height : width);

    // BitmapFactory.Options options = new BitmapFactory.Options();
    // options.inSampleSize = 2;
    // Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
    // mResultImageView.setImageBitmap(bm);

    // The ImageFetcher takes care of loading images into our ImageView
    // children asynchronously

    mImageFetcher = new ImageFetcher(this, longest);

    mImageFetcher.loadImage(mCurrentPhotoPath, mResultImageView);
    final Activity activity = this;
    ViewTreeObserver viewTreeObserver = mResultImageView.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
      viewTreeObserver.addOnGlobalLayoutListener(
          new OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
              if (mRectLeft != 0) {
                Log.d(Constants.DEBUG, "CROP rect set mrectleft" + mRectLeft);
                mCropRectangle.setCropRectangle(
                    new RectF(mRectLeft, mRectTop, mRectRight, mRectBottom));
              }
              mCropRectangle.setReportView(mResultImageView, activity);
              mResultImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
          });
    }
  }