Transform original bitmap on android -


so, i'm using library https://github.com/thuytrinh/android-collage-views add "multitouchlistener" feature imageview. let user modify photo needs using rotation, scale , translation. problem how save it. did 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, image not big enough - it's big view on phone depends on screen resolution. , want "apply" these transformations on given bitmap full size. , want transformed image on screen (so it'll need crop out of screen)

i tried 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 doesn't expected.

user screen:

enter image description here

and output image (without cropping, because don't want side should crop):

enter image description here

how can fix this? there solution?

here 1 way it, not perfect should give start :

in this, container refers view contains transformed imageview, phone case on screenshot , src raw source bitmap.

  • first, need compute desired width , height of output bitmap, i.e size make image fit in while keeping ratio of container :

    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; } 
  • apply ratio between container width/height , output width/height translation part of matrix hold transformation user did

    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); 
  • draw output bitmap doing correct size (outputwidth, outputheight) , matrix (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.

warnings

you should careful memory allocation, code lead allocate massive bitmaps, should implement kind of limit along needs. (also allocation , drawing in background)

you might need adjustment depending on place image in first place.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -