Convert original bitmap to android

So, I use this library https://github.com/thuytrinh/android-collage-views to add the MultiTouchListener function to my ImageView. Basically, I allow the user to modify the photo according to his needs through rotation, scaling and translation. Now the only problem is how to save it. I did it like this:

    Bitmap bitmap = Bitmap.createBitmap(imageContainer.getWidth(),                         imageContainer.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);
    imageContainer.draw(canvas);

It works, but the image is not big enough - it is as big as on the phone, so it depends on the screen resolution. And I want to "apply" these transformations to a given full-size bitmap. And I want the converted image to look like on the screen (so that it needs to crop everything from the screen)

I tried the following:

    Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(),         image.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(Color.WHITE);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    canvas.drawBitmap(image, imageView.getMatrix(), paint);

But this does not look as expected.

User Screen:

enter image description here

And output the image (without cropping, because I don't want which side I have to crop):

enter image description here

How can i fix this? Is there any solution?

+4
source share
1 answer

Here is one way to do this, definitely not perfect, but should give you a good start:

This containerrefers to a view that contains a converted ImageView, telephone case in a screenshot and a srcbitmap of the source.

  • First, you need to calculate the required width and height of the output raster image, that is, the size that the image would have to make in it, while maintaining the container ratio:

    float containerWidth = containerView.getWidth();
    float containerHeight = containerView.getHeight();
    
    float srcWidth = src.getWidth();
    float srcHeight = src.getHeight();
    
    float containerRatio = containerWidth / containerHeight;
    float srcRatio = srcWidth / srcHeight;
    
    float outputWidth, outputHeight;
    
    if(srcRatio > containerRatio) { //fits in width
            outputWidth = srcWidth;
            outputHeight = srcWidth / containerRatio;
    }
    else if(srcRatio < containerRatio) { //fits in height
            outputHeight = srcHeight;
            outputWidth = srcHeight * containerRatio;
    }
    else {
        outputWidth = srcWidth;
        outputHeight = srcHeight;
    }
    
  • / / , ,

    float containerToOutputRatioWidth = outputWidth / containerWidth;
    float containerToOutputRatioHeight = outputHeight / containerHeight;
    
    float[] values = new float[9];
    transformedImageView.getMatrix().getValues(values);
    values[2] = values[2] * containerToOutputRatioWidth;
    values[5] = values[5] * containerToOutputRatioHeight;
    Matrix outputMatrix = new Matrix();
    outputMatrix.setValues(values);
    
  • , , (outputWidth, outputHeight) (outputMatrix).

    Bitmap newBitmap = Bitmap.createBitmap(Math.round(outputWidth), Math.round(outputHeight), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(Color.WHITE);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    canvas.drawBitmap(src, outputMatrix, paint);
    

Demo.

, , - , . ( )

.

+4

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


All Articles