Crop Square Image to Circle - Programmatically

Crop square image to circle - Programmatically

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DrawingView dv = new DrawingView(this);
setContentView(dv);
}

class DrawingView extends View {
Bitmap bitmap;

public DrawingView(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.glossy_overlay);

}

@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
// paint.setColor(Color.CYAN);
canvas.drawBitmap(getclip(), 30, 20, paint);
}

public Bitmap getclip() {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
// paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2,
bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
}

iPhone programmatically crop a square image to appear as circle

Yes you can use CoreGraphics to draw the mask dynamically.
Then you can create the masked image.

Example for masking:

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage
{
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
CGImageRelease(maskedImageRef);
CGImageRelease(mask);
return maskedImage;
}

Cropping square image into circular image in android

Pay attention to your code. You are using 2 bitmaps in memory.

There is a very interesting post about image with rounded corners: http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

It is written by Romain Guy (ex android team at Google).

You can write a circular bitmap with a similar code:

public class CircleDrawable extends Drawable {

private final BitmapShader mBitmapShader;
private final Paint mPaint;
private Paint mWhitePaint;
int circleCenterX;
int circleCenterY;
int mRadus;
private boolean mUseStroke = false;
private int mStrokePadding = 0;

public CircleDrawable(Bitmap bitmap) {

mBitmapShader = new BitmapShader(bitmap,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(mBitmapShader);

}

public CircleDrawable(Bitmap bitmap, boolean mUseStroke) {
this(bitmap);

if (mUseStroke) {
this.mUseStroke = true;
mStrokePadding = 4;
mWhitePaint = new Paint();
mWhitePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mWhitePaint.setStrokeWidth(0.75f);
mWhitePaint.setColor(Color.WHITE);
}
}

@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
circleCenterX = bounds.width() / 2;
circleCenterY = bounds.height() / 2;

if (bounds.width() >= bounds.height())
mRadus = bounds.width() / 2;
else
mRadus = bounds.height() / 2;
}

@Override
public void draw(Canvas canvas) {
if (mUseStroke) {
canvas.drawCircle(circleCenterX, circleCenterY, mRadus, mWhitePaint);
}
canvas.drawCircle(circleCenterX, circleCenterY, mRadus - mStrokePadding, mPaint);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}

public boolean ismUseStroke() {
return mUseStroke;
}

public void setmUseStroke(boolean mUseStroke) {
this.mUseStroke = mUseStroke;
}

}

To use it:

CircleDrawable circle = new CircleDrawable(bitmap,true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
imageView.setBackground(circle);
else
imageView.setBackgroundDrawable(circle);

How to crop a square Image and change it into a circle C#

UPDATE thanks to @TaW I've updated g.SetClip(path) instead of new region

  1. Make a new Bitmap that matches the original in size and pixel
    format.
  2. Create a graphics from that new Bitmap.
  3. Set the graphics

  4. clip to a new circle

  5. Draw the original image onto the new graphics.

Here is an example:

public Bitmap ClipToCircle(Bitmap original, PointF center, float radius)
{
Bitmap copy = new Bitmap(original);
using (Graphics g = Graphics.FromImage(copy)) {
RectangleF r = new RectangleF(center.X - radius, center.Y - radius,
radius * 2, radius * 2);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(r);
g.SetClip(path)
g.DrawImage(original, 0, 0);
return copy;
}
}

I hope it solves your issue.

How to crop a circular image to inscribed square, then crop to inscribed circle, and finally crop to inscribed square?

The image you want to crop to is, geometrically, a square centered on the input image, half as large. This is because you're inscribing twice, each time the square shrinks by the square root of two, and dividing by SQRT(2) twice is the same thing as dividing by 2.

So if you have an input square of side D (or a circular image of diameter D), what you need to do is crop with center (D/2, D/2) and a side of D/2.

Most efficient way to crop image to circle (in R)?

You can improve the performance of your circ function if you do a vectorised subset-assign operation on your array (instead of looping) using the the fact that (x-xc)^2 +(y-yc)^2 > r^2 for points outside a circle.

To do this, replace the 2nd part of your function with

  # Second part of the function traces circle by...
x = rep(1:xmax, ymax)
y = rep(1:ymax, each=xmax)
r2 = r^2
ma[,,4][which(( (x-xc)^2 + (y-yc)^2 ) > r2)] <- 0
return(ma)

How to crop circular area from bitmap in Android

After long brainstorming I have found the solution

public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}

Libgdx crop image to circle

I use this, it works, but there is no interpolation, which means the edges are pixelated. You can chose to either implement interpolation or use a border!

public static Pixmap roundPixmap(Pixmap pixmap)
{
int width = pixmap.getWidth();
int height = pixmap.getHeight();
Pixmap round = new Pixmap(pixmap.getWidth(),pixmap.getHeight(),Pixmap.Format.RGBA8888);
if(width != height)
{
Gdx.app.log("error", "Cannot create round image if width != height");
round.dispose();
return pixmap;
}
double radius = width/2.0;
for(int y=0;y<height;y++)
{
for(int x=0;x<width;x++)
{
//check if pixel is outside circle. Set pixel to transparant;
double dist_x = (radius - x);
double dist_y = radius - y;
double dist = Math.sqrt((dist_x*dist_x) + (dist_y*dist_y));
if(dist < radius)
{
round.drawPixel(x, y,pixmap.getPixel(x, y));
}
else
round.drawPixel(x, y, 0);
}
}
Gdx.app.log("info", "pixmal rounded!");
return round;
}

Do keep in mind this will all be unmanaged! To make life easier I usually save the image and load it as a managed texture.



Related Topics



Leave a reply



Submit