<commit_before>/*
 * Copyright (C) 2007 The Android Open Source Project
 * Copyright (C) 2012,2013 <NAME>
 *
 * 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.renard.ocr.cropimage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.googlecode.leptonica.android.Box;
import com.googlecode.leptonica.android.Clip;
import com.googlecode.leptonica.android.Pix;
import com.googlecode.leptonica.android.Rotate;
import com.googlecode.leptonica.android.Scale;
import com.googlecode.leptonica.android.WriteFile;
import com.renard.ocr.DocumentGridActivity;
import com.renard.ocr.R;
import com.renard.ocr.help.HintDialog;
import com.renard.util.Util;

/**
 * The activity can crop specific region of interest from an image.
 */
public class CropImage extends MonitoredActivity {
	private static final int HINT_DIALOG_ID = 2;

	private int mAspectX, mAspectY;
	private final Handler mHandler = new Handler();

	private int mRotation = 0;

	boolean mSaving; // Whether the "save" button is already clicked.
	private Pix mPix; // original Picture
	private Pix mPixScaled; // scaled Picture
	private CropImageView mImageView;

	private Bitmap mBitmap;
	private HighlightView mCrop;
	private float mScaleFactor = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		getWindow().setFormat(PixelFormat.RGBA_8888);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.cropimage_activity);

		// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		// getSupportActionBar().setDisplayShowHomeEnabled(true);

		mImageView = (CropImageView) findViewById(R.id.image);
		mImageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
                //TODO not run on UI Thread
				Intent intent = getIntent();
				Bundle extras = intent.getExtras();
				if (extras != null) {
					// mImagePath = extras.getString("image-path");
					// mSaveUri = Uri.fromFile(new File(mImagePath));
					// mBitmap = getBitmap(mImagePath);
					mPix = new Pix(extras.getInt(DocumentGridActivity.EXTRA_NATIVE_PIX));

					// scale it so that it fits the screen
					
					float bestScale = 1/getScaleFactorToFitScreen(mPix, mImageView.getWidth(), mImageView.getHeight());
                    mScaleFactor = Util.determineScaleFactor(mPix.getWidth(),mPix.getHeight(),mImageView.getWidth(), mImageView.getHeight());

                    if (mScaleFactor==0){
                        mScaleFactor=1;
                    } else {
                        if (bestScale<1 && bestScale>0.5f){
                            mScaleFactor=0.5f;
                        } else if (bestScale<=0.5f){
                            mScaleFactor=0.25f;
                        }
                        mScaleFactor = (float) (1/Math.pow(2,mScaleFactor));
                    }

					mPixScaled = Scale.scale(mPix, mScaleFactor);

					mBitmap = WriteFile.writeBitmap(mPixScaled);
					mAspectX = extras.getInt("aspectX");
					mAspectY = extras.getInt("aspectY");
					mRotation = extras.getInt(DocumentGridActivity.EXTRA_ROTATION) / 90;
				}<commit_msg>don’t scale too much when cropping<commit_after>/*
 * Copyright (C) 2007 The Android Open Source Project
 * Copyright (C) 2012,2013 <NAME>
 *
 * 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.renard.ocr.cropimage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.googlecode.leptonica.android.Box;
import com.googlecode.leptonica.android.Clip;
import com.googlecode.leptonica.android.Pix;
import com.googlecode.leptonica.android.Rotate;
import com.googlecode.leptonica.android.Scale;
import com.googlecode.leptonica.android.WriteFile;
import com.renard.ocr.DocumentGridActivity;
import com.renard.ocr.R;
import com.renard.ocr.help.HintDialog;
import com.renard.util.Util;

/**
 * The activity can crop specific region of interest from an image.
 */
public class CropImage extends MonitoredActivity {
	private static final int HINT_DIALOG_ID = 2;

	private int mAspectX, mAspectY;
	private final Handler mHandler = new Handler();

	private int mRotation = 0;

	boolean mSaving; // Whether the "save" button is already clicked.
	private Pix mPix; // original Picture
	private Pix mPixScaled; // scaled Picture
	private CropImageView mImageView;

	private Bitmap mBitmap;
	private HighlightView mCrop;
	private float mScaleFactor = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		getWindow().setFormat(PixelFormat.RGBA_8888);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.cropimage_activity);

		// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		// getSupportActionBar().setDisplayShowHomeEnabled(true);

		mImageView = (CropImageView) findViewById(R.id.image);
		mImageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
                //TODO not run on UI Thread
				Intent intent = getIntent();
				Bundle extras = intent.getExtras();
				if (extras != null) {
					// mImagePath = extras.getString("image-path");
					// mSaveUri = Uri.fromFile(new File(mImagePath));
					// mBitmap = getBitmap(mImagePath);
					mPix = new Pix(extras.getInt(DocumentGridActivity.EXTRA_NATIVE_PIX));

					// scale it so that it fits the screen
					
					float bestScale = 1/getScaleFactorToFitScreen(mPix, mImageView.getWidth(), mImageView.getHeight());
                    mScaleFactor = Util.determineScaleFactor(mPix.getWidth(),mPix.getHeight(),mImageView.getWidth(), mImageView.getHeight());

                    if (mScaleFactor==0){
                        mScaleFactor=1;
                    } else {
                        if (bestScale<1 && bestScale>0.5f){
                            mScaleFactor = (float) (1/Math.pow(2,0.5f));
                        } else if (bestScale<=0.5f){
                            mScaleFactor = (float) (1/Math.pow(2,0.25f));
                        } else {
                            mScaleFactor = (float) (1/mScaleFactor);
                        }
                    }

					mPixScaled = Scale.scale(mPix, mScaleFactor);

					mBitmap = WriteFile.writeBitmap(mPixScaled);
					mAspectX = extras.getInt("aspectX");
					mAspectY = extras.getInt("aspectY");
					mRotation = extras.getInt(DocumentGridActivity.EXTRA_ROTATION) / 90;
				}
{'fraction_non_alphanumeric': 0.09938837920489296, 'fraction_numerical': 0.009762408844977652, 'mean_word_length': 3.405699481865285, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1884731', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2413, 'n_words': 616}