I'm trying to crop a picture from camera but it is shown zoomed. the width and height are ok but it's zoomed a lot.
This is my code :
This is the code I use for scaling bitmaps, the wantedWidth and wantedHeight would be the width and height of your view.
public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Matrix m = new Matrix();
m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
canvas.drawBitmap(bitmap, m, new Paint());
return output;
}
Related
I want to crop a picture in current window,maybe circle or rectangle.
Here are my code.I first get the whole picture in current window.Then to crop circle or rectangle.
private Bitmap getBitmap()
{
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap mBackgroundBitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
mRect = ChooseView.cropRect; // the mRect is the crop area
int width = mRect.width();
int height = mRect.height();
Bitmap whiteBgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(whiteBgBitmap);
Rect dstRect = new Rect(0, 0, width, height);
if(!TextUtils.isEmpty(CutParam.shape) && CutParam.shape.equals(CutParam.RECTANGLE))
{
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mBackgroundBitmap, mRect, dstRect, null);
}
else
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(mRect.left + mRect.width() / 2, mRect.top + mRect.height() / 2, CutParam.radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(mBackgroundBitmap, mRect, dstRect, null);
}
return whiteBgBitmap;
}
The rectangle work will.But when I want to crop a circle,it will be a black image. I am fresh at Android.And I have read Cropping circular area from bitmap in Android .But I can't solve my question.Can you help me?
PS:If crop a circle ,the mRect will be a square.
Try this one from lvillani on github. It's simple, and gets you out from the muddy world of pictures in android.
I have a square bitmap being displayed underneath a semi-transparent circle. The user can touch and drag the bitmap to position it. I want to be able to crop what ever part of the bitmap is under the circle. How can I do this?
have a look at RoundedBitmapDrawable in the support library
all you have to do is give it the bitmap and the corner radius
RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create(getResources(),bitmap);
img.setCornerRadius(radius);
imageView.setImageDrawable(img);
You Can make your imageview circular using RoundedBitmapDrawable
here is the code for achieving roundedImageview:
ImageView profilePic=(ImageView)findViewById(R.id.user_image);
//get bitmap of the image
Bitmap imageBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
RoundedBitmapDrawable roundedBitmapDrawable=
RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
roundedBitmapDrawable.setCornerRadius(50.0f);
roundedBitmapDrawable.setAntiAlias(true);
profilePic.setImageDrawable(roundedBitmapDrawable);
You can use the power of PorterDuff to get your bitmap in any shape or path...
Here is an example:
public static Bitmap getCircular(Bitmap bm, int cornerRadiusPx) {
int w = bm.getWidth();
int h = bm.getHeight();
int radius = (w < h) ? w : h;
w = radius;
h = radius;
Bitmap bmOut = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOut);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xff424242);
Rect rect = new Rect(0, 0, w, h);
RectF rectF = new RectF(rect);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(rectF.left + (rectF.width()/2), rectF.top + (rectF.height()/2), radius / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bm, rect, rect, paint);
return bmOut;
}
Here is a link to a sample project. It has a transparent square over an image. You can pinch zoom or drag the bottom image and can corp it.
https://github.com/tcking/ImageCroppingView.
The square is made by using canvas. you can change it to any shape as u desired by changing canvas. Hop it helps you.
please help me to draw a text with rounded rectangle as background. I have to draw many texts on a canvas and the text has rounded background. SO what I am trying is to write a function "createTextBitmap" which return a bitmap image so that we can draw image(which return by the function) on a main canvas.
the 'createTextBitmap'function can return a created bitmap, the bitmap image is the one,which contains the text with rounded edge background...
i have tried one, which gives below.
private Bitmap ProcessingBitmap(String text,Paint paint, boolean lastPoint){
Bitmap bm1 = null;
Bitmap newBitmap = null;
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float width = bounds.width();
float height =bounds.height();
float radius;
if (width > height){
radius = height/4;
}else{
radius = width/4;
}
Paint paint1 = new Paint();
paint1.setColor(Color.GREEN);
paint1.setStrokeWidth(5);
paint1.setStyle(Paint.Style.FILL);
float center_x, center_y;
center_x = width/4;
center_y = height/4;
final RectF rect = new RectF();
rect.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
Canvas canvas2 = new Canvas();
canvas2.drawRoundRect(rect, 0, 0, paint);
canvas2.drawText(text, 0, 0, paint);
return newBitmap;
}
and my question is How can we convert this canvas2 to a bitmap image? and image has the size of text bounds,
which look like
to convert your canvas into bitmap please do the following :
public Bitmap convertCanvasToBitmap(int width , int height) {
Bitmap drawnBitmap = null;
try {
drawnBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(drawnBitmap);
// now draw anything you want to the canvas
} catch (Exception e) {
e.printStackTrace();
}
return drawnBitmap;
}
so the idea is just pass the bitmap to the canvas , draw with the canvas it will be drawn into your bitmap .
and please refer to this answer here to see how to deal with the text size in the bitmap .
and please give me some feedback
Hope that helps .
you can create a bitmap, then call draw on that bitmap, something like this:
newBitmap = Bitmap.createBitmap(rect.width, rect.height, Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(newBitmap);
I wan to draw a image (get from camera) on surfaceview. After drawing the image i want to reduce the size of the image, change the image location on surface view with touch. rotate the image. currently i draw the image on surface view but how the remaining task is not done. Please some body help me.
Thanks
public void drawImage(Canvas canvas, Bitmap bitmap, int x, int y, int rotationAngle, float scalingfactor){
Matrix matrix = new Matrix();
matrix.postRotate(rotationAngle, bitmap.getWidth()/2, bitmap.getHeight()/2);
matrix.postScale(scalingfactor, scalingfactor, bitmap.getWidth()/2, bitmap.getHeight()/2);
matrix.postTranslate(x, y);
canvas.drawBitmap(bitmap, matrix, null);
}
Use above code to rotate and scaling image in surfaceview.
if scalingfactor 1.0f means, image size will be same. If you want to reduce the size use like 0.5f.
rotationAngle is 0 to 360
This is more flexible, you can draw bitmap based on scaling or specific width and height:
public void drawImage(Bitmap bitmap, int x, int y, int rotationAngle, float scalingfactor, int width, int height){
Matrix matrix = new Matrix();
matrix.postRotate(rotationAngle, bitmap.getWidth()/2, bitmap.getHeight()/2);
if(scalingfactor == 0) {
matrix.postScale((float) width / bitmap.getWidth(), (float) height / bitmap.getHeight());
}
else
matrix.postScale(scalingfactor, scalingfactor, bitmap.getWidth()/2, bitmap.getHeight()/2);
dstRectF.set(x, y, x + width, y + height);
matrix.postTranslate(x, y);
canvas.drawBitmap(bitmap, matrix, null);
}
I have read those posts on this issue but my case is abit different as I am NOT DISPLAYING the bitmap but just post-processing the image from raw data.
First, I call ImageProcessing.rgbToBitmap(data,width, height); which will return a Bitmap object. Then I perform these 3 functions SEPARATELY.
Rotate Bitmap
Add a watermark overlay to Bitmap
Add date at lower right hand corner of Bitmap
All 3 methods called will create an return a Bitmap object which probably causes the crash as I am trying to save an image every 1000ms! Sometimes the images saved are distorted probably due to the memory error.
I am posting my codes below and any advices are greatly appreciated. I do not want to compromise on the quality on the image taken though. (Need to preserve the resolution)
public static Bitmap addWatermark(Resources res, Bitmap source) {
int w, h;
Canvas c;
Paint paint;
Bitmap bmp, watermark;
Matrix matrix;
float scale;
RectF r;
w = source.getWidth();
h = source.getHeight();
// Create the new bitmap
bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG
| Paint.FILTER_BITMAP_FLAG);
// Copy the original bitmap into the new one
c = new Canvas(bmp);
c.drawBitmap(source, 0, 0, paint);
// Load the watermark
watermark = BitmapFactory.decodeResource(res, R.drawable.watermark);
// Scale the watermark to be approximately 10% of the source image
// height
scale = (float) (((float) h * 0.80) / (float) watermark.getHeight());
// Create the matrix
matrix = new Matrix();
matrix.postScale(scale, scale);
// Determine the post-scaled size of the watermark
r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
matrix.mapRect(r);
// Center watermark
matrix.postTranslate((w - r.width()) / 2, (h - r.height()) / 2);
// Draw the watermark
c.drawBitmap(watermark, matrix, paint);
// Free up the bitmap memory
watermark.recycle();
return bmp;
}
public static Bitmap addDate(Bitmap src, String date) {
int w = src.getWidth();
int h = src.getHeight();
//Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Bitmap result = src;
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.rgb(255, 185, 15));
paint.setTextSize(20);
paint.setAntiAlias(true);
canvas.drawText(date, w - 200, h - 20, paint);
return result;
}
public static Bitmap rotate(Bitmap src, int rotation) {
int width = src.getWidth();
int height = src.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// rotate the Bitmap
matrix.postRotate(rotation);
// recreate the new Bitmap, swap width and height and apply
// transform
Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height,
matrix, true);
return rotatedBitmap;
}
Try this first:
Change
// Copy the original bitmap into the new one
c = new Canvas(bmp);
c.drawBitmap(source, 0, 0, paint);
with
// Copy the original bitmap into the new one
c = new Canvas(bmp);
bmp.recycle(); //here or below
c.drawBitmap(source, 0, 0, paint);
//below bmp.recycle();
and here:
canvas.drawText(date, w - 200, h - 20, paint);
return result;
with
canvas.drawText(date, w - 200, h - 20, paint);
result.recycle();
return result;
and here
Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height,
matrix, true);
return rotatedBitmap;
with
Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height,
matrix, true);
return rotatedBitmap;
rotatedBitmap.recycle();
Add all this (recycle();) maybe this is already enough and also try the code with smaller bitmaps, en see if it still crashes (like 50px by 50px).
And no, their is no way to increase the VM of your phone.