How to limit the minimum crop size of the image in android

There is quite a bit of talk about how to crop an image taken from a gallery or camera. A very simple, which not many people include the following snippet

intent.putExtra("crop", "true");
intent.putExtra("aspectX", 617);
intent.putExtra("aspectY", 619);

(In any case, the fragment must be incomplete. Moving ...)

Notice how in the fragment I use two large prime numbers. I was here, hoping to limit the minimum crop size. 617X619 does not seem to have any effect; which for me basically means that any algorithm has been used, has a function that is similar to

double scale =  (double) aspectY/aspectX;

which is then used to map xDimen to yDimen.

But before throwing my hands into the air and giving up, I noticed that instagram successfully managed to limit the minimum size of the trimmer. Does anyone know how I can do this? Basically, if you use instagram, you will notice that you cannot crop the “too small” part of the image. How can I solve these measurements?

Basically, as if instagram does

  double scale =  (double) aspectY/aspectX;
  //....
  if(someInputX > aspectX){
    xDimen = someInputX;
    yDimen = scale* xDimen;
  }else{
    //do not crop any further
  }

I looked at several cropping libraries to find out if I can figure out how to fix the minimum sizes, but I can't figure out how to figure it out. For example, I studied git projects related to http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

+4
source share
2
+2

Instagram . , . . MotionEvent Bitmap, 1 .

:

public class MainActivity extends Activity {
private ImageView imageView;
private ViewManager viewManager;
private Matrix matrix;
private int size;
private final int outputSize = 100;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    size = width < height ? width : height;
    size -= 50;

    imageView = (ImageView) findViewById(R.id.imageViewCrop);
    imageView.getLayoutParams().width = size;
    imageView.getLayoutParams().height = size;
    viewManager = (ViewManager) imageView.getParent();

    if (getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)) {
        createZoomControls();
    }

    imageView.setOnTouchListener(new OnTouchListener() {
        float initX;
        float initY;
        float midX;
        float midY;
        float scale;
        float initDistance;
        float currentDistance;
        boolean isMultitouch = false;

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                initX = event.getX();
                initY = event.getY();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                isMultitouch = true;
                initDistance = (float) Math.sqrt(Math.pow(
                        initX - event.getX(1), 2)
                        + Math.pow(initY - event.getY(1), 2));
                break;
            case MotionEvent.ACTION_MOVE:
                if (isMultitouch) {
                    matrix = imageView.getImageMatrix();
                    currentDistance = (float) Math.sqrt(Math.pow(initX
                            - event.getX(1), 2)
                            + Math.pow(initY - event.getY(1), 2));
                    scale = 1 + 0.001f * (currentDistance - initDistance);
                    midX = 0.5f * (initX + event.getX(1));
                    midY = 0.5f * (initY + event.getY(1));
                    matrix.postScale(scale, scale, midX, midY);
                    imageView.setImageMatrix(matrix);
                    imageView.invalidate();
                } else {
                    imageView.scrollBy((int) (initX - event.getX()),
                            (int) (initY - event.getY()));
                    initX = event.getX();
                    initY = event.getY();
                }
                break;
            case MotionEvent.ACTION_UP:
                isMultitouch = false;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                isMultitouch = false;
                break;
            }
            return true;
        }
    });
}

public void createZoomControls() {
    ZoomButtonsController zoomButtonsController = new ZoomButtonsController(
            imageView);
    zoomButtonsController.setVisible(true);
    zoomButtonsController.setAutoDismissed(false);
    zoomButtonsController.setOnZoomListener(new OnZoomListener() {

        public void onZoom(boolean zoomIn) {
            matrix = imageView.getImageMatrix();
            if (zoomIn) {
                matrix.postScale(1.05f, 1.05f, 0.5f * size, 0.5f * size);
                imageView.setImageMatrix(matrix);
            } else {
                matrix.postScale(0.95f, 0.95f, 0.5f * size, 0.5f * size);
                imageView.setImageMatrix(matrix);
            }
            imageView.invalidate();
        }

        public void onVisibilityChanged(boolean visible) {
        }
    });
    RelativeLayout.LayoutParams zoomLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    zoomLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    zoomLayoutParams.addRule(RelativeLayout.BELOW, R.id.imageViewCrop);
    viewManager.addView(zoomButtonsController.getContainer(),
            zoomLayoutParams);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    Toast.makeText(this, R.string.info, 5).show();
    return super.onMenuItemSelected(featureId, item);
}

public void buttonPickClick(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (resultCode) {
    case RESULT_OK:
        Uri targetUri = data.getData();
        imageView.setScaleType(ScaleType.CENTER_INSIDE);
        imageView.scrollTo(0, 0);
        imageView.setImageURI(targetUri);
        imageView.setScaleType(ScaleType.MATRIX);
        break;
    }
}

public void buttonCropClick(View view) throws IOException {
    imageView.setDrawingCacheEnabled(true);
    imageView.buildDrawingCache(true);
    File imageFile = new File(Environment.getExternalStorageDirectory(),
            "Pictures/" + UUID.randomUUID().toString() + ".jpg");
    FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
    Bitmap.createScaledBitmap(imageView.getDrawingCache(true), outputSize,
            outputSize, false).compress(CompressFormat.JPEG, 100,
            fileOutputStream);
    fileOutputStream.close();
    imageView.setDrawingCacheEnabled(false);
}
}

http://developer.android.com/training/displaying-bitmaps/index.html

+1

Source: https://habr.com/ru/post/1543128/


All Articles