java - Cropping image lowers quality and border looks bad -


using math, created following java-function, input bitmap, , have crop out centered square in circle cropped out again black border around it. rest of square should transparent. additionatly, there transparent distance sides not damage preview when sending image via messengers.

the code of function following:

 public static bitmap edit_image(bitmap src,boolean makeborder) {         int width = src.getwidth();         int height = src.getheight();         int a, r, g, b;         int pixel;          int middlex = width/2;         int middley = height/2;          int seitenlaenge,startx,starty;         if(width>height)          {             seitenlaenge=height;             starty=0;              startx = middlex - (seitenlaenge/2);         }         else          {             seitenlaenge=width;              startx=0;              starty = middley - (seitenlaenge/2);         }          int kreisradius = seitenlaenge/2;         int mittx = startx + kreisradius;         int mitty = starty + kreisradius;         int border=2;         int seitenabstand=55;          bitmap bmout = bitmap.createbitmap(seitenlaenge+seitenabstand, seitenlaenge+seitenabstand, bitmap.config.argb_8888);         bmout.sethasalpha(true);          for(int x = 0; x < width; ++x) {             for(int y = 0; y < height; ++y) {                 int distzumitte = (int) (math.pow(mittx-x,2) + math.pow(mitty-y,2)); // (xm-xp)^2 + (ym-yp)^2 = dist^2                 distzumitte = (int) math.sqrt(distzumitte);                  pixel = src.getpixel(x, y);                  = color.alpha(pixel);                 r = (int)color.red(pixel);                 g = (int)color.green(pixel);                 b = (int)color.blue(pixel);                 int color = color.argb(a, r, g, b);                  int afterx=x-startx+(seitenabstand/2);                 int aftery=y-starty+(seitenabstand/2);                  if(x < startx || y < starty || afterx>=seitenlaenge+seitenabstand || aftery>=seitenlaenge+seitenabstand) //seitenrand                 {                     continue;                 }                 else if(distzumitte > kreisradius)                 {                     color=0x00ffffff;                 }                 else if(distzumitte > kreisradius-border && makeborder) //border                 {                     color = color.argb(a, 0, 0, 0);                 }                 bmout.setpixel(afterx, aftery, color);             }         }          return bmout;     } 

this function works fine, there problems occuring wasn't able resolve yet.

  • the quality of image decreased significantly
  • the border not round, appears flat @ edges of image (on devices?!)

i'd appreciate regarding problems. got admit i'm not best in math , there should better formula ceate border.

your source code hard read, since mix of german , english in variable names. additionally don't image library use, don't know classes bitmap , color come from.

anyway, obvious, operating on bitmap. bitmap means whole image stored in ram pixel pixel. there no lossy compression. don't see in source code, can affect quality of image.

it likely, answer in code don't show us. additionally, describe (botrh of problems) sounds typical low quality jpeg compression. sure, somewhere after call function, convert/save image jpeg. try @ position bmp, tiff or png , see error disappears magically. maybe can set quality level of jpeg somewhere avoid that.

to make easier others (maybe) find answer, please allow me translate code english:

    public static bitmap edit_image(bitmap src,boolean makeborder) {         int width = src.getwidth();         int height = src.getheight();         int a, r, g, b;         int pixel;          int middlex = width/2;         int middley = height/2;          int sidelength,startx,starty;         if(width>height)          {             sidelength=height;             starty=0;              startx = middlex - (sidelength/2);         }         else          {             sidelength=width;              startx=0;              starty = middley - (sidelength/2);         }          int circleradius = sidelength/2;         int middlex = startx + circleradius;         int middley = starty + circleradius;         int border=2;         int sidedistance=55;          bitmap bmout = bitmap.createbitmap(sidelength+sidedistance, sidelength+sidedistance, bitmap.config.argb_8888);         bmout.sethasalpha(true);          for(int x = 0; x < width; ++x) {             for(int y = 0; y < height; ++y) {                 int distancetomiddle = (int) (math.pow(middlex-x,2) + math.pow(middley-y,2)); // (xm-xp)^2 + (ym-yp)^2 = dist^2                 distancetomiddle = (int) math.sqrt(distancetomiddle);                  pixel = src.getpixel(x, y);                  = color.alpha(pixel);                 r = (int)color.red(pixel);                 g = (int)color.green(pixel);                 b = (int)color.blue(pixel);                 int color = color.argb(a, r, g, b);                  int afterx=x-startx+(sidedistance/2);                 int aftery=y-starty+(sidedistance/2);                  if(x < startx || y < starty || afterx>=sidelength+sidedistance || aftery>=sidelength+sidedistance) //margin                 {                     continue;                 }                 else if(distancetomiddle > circleradius)                 {                     color=0x00ffffff;                 }                 else if(distancetomiddle > circleradius-border && makeborder) //border                 {                     color = color.argb(a, 0, 0, 0);                 }                 bmout.setpixel(afterx, aftery, color);             }         }          return bmout;     } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -