// 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); }
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(); } }