I have been searching the net for this but couldn't find the any help.
I want to capture an Image by invoking camera and want to crop it. But the thing is instead of RECTANGULAR crop( given in image) tool I need a CIRCULAR one (second image).
Note: First image shows - crop rectangular area and then using some other function displays it in circular fashion.
while image two shows - crop image in circular shape.
All I found on net was to crop using this rectangular tool and then display the image in circular view. (But I want to crop in circular form)
What I am doing in onActivityResult function is-
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null)
{
Bitmap photo = extras.getParcelable("data");
//this method convert the rectangular cropped image to circular display.
GraphicsUtil gu= new GraphicsUtil();
Bitmap output = gu.getCircleBitmap(photo,16);
mImageView.setImageBitmap(output);
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
break;
Code for GraphicsUtil function is as below -
public Bitmap getCircleBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffff0000;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 4);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
So you can see. Image is cropped rectangular then passed to a function which display it in circular fashion.
I want that image is cropped in circular way by CROP_FROM_CAMERA intent only without using GraphicUtil function.
Since different manufacturers implement their own interfaces for camera, the first method below will not work in all cases. For example, it worked on my Nexus S running stock 2.3.6 and not on my Galaxy Nexus on stock 4.3. Anyway, if you want to be able to crop your image in a circular fashion from the default crop action call it in the following way:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
intent.setData(imageUri); // Uri to the image you want to crop
intent.putExtra("outputX", Constants.PROFILE_PICTURE_SIZE);
intent.putExtra("outputY", Constants.PROFILE_PICTURE_SIZE);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("circleCrop", new String(""));
intent.putExtra("return-data", false);
File cropImageFile = new File(CROP_IMAGE_PATH); // Path to save the cropped image
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropImageFile));
startActivityForResult(intent, CROP); // CROP = Code to track the result in onActivityResult
This should start the default crop app that will look like this:
If you've read this post by commonsware, you know that this will not always work. I suggest including one of the libraries from his post. The first one from lvillani is the CropImage activity extracted AOSP. Include the library and make call it with an intent similar to the one above. Make sure you include the circleCrop param in the intent.
BitmapShader shader;
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
RectF rect = new RectF(0.0f, 0.0f, width, height);
Bitmap circularBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(circularBitmap);
canvas.drawArc(rect, 0, 360, true, paint);
now circularBitmap is holding the circular image.
I haven't tried this yet, but it should work...
I will assume you can get the image from the camera. Once you have that image, you can proceed as follows.
Get a Canvas to draw on a new bitmap. Create a Path object and call addCircle. Call clipPath on the Canvas passing your circular Path. Now draw the camera image to the Canvas and it should only draw inside the clipping area. Unlock the Canvas and use the new Bitmap to save out whatever type of image you want. Dispose of whatever resources as appropriate.
Related
This question already has answers here:
How to draw text On image?
(7 answers)
Closed 2 years ago.
I am new for android and i want to add watermark effect on captured images and i am trying find the solution for it since 2 days but still not able to add watermark effect on captured images can some one help me what i did mistack in my below code..
I want add water mark effect on bottom middle of image
Code
private void onCaptureImageResult(Intent data) {
Bitmap photo;Bitmap watermark_bitmap=null;
String selectedFilePath = null;
if (data.getExtras().get("data") != null) {
photo = (Bitmap) data.getExtras().get("data");
watermark_bitmap=Utilities.mark(this,photo,"Hello World",true);
Uri contentUri = Utilities.getImageUri1(
this, watermark_bitmap);
selectedFilePath = ImageFilePath.getPath(
this, contentUri);
newMeter_image.setImageBitmap(watermark_bitmap);
}
public static Bitmap mark(Context context,Bitmap src, String watermark, boolean underline) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
int color = ContextCompat.getColor(context, R.color.black);
paint.setColor(color);
paint.setAlpha(1);
paint.setTextSize(5);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, 50,50, paint);
return result;
}
I would have also used the same approach.
Load the image in a canvas.
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Set the paint and draw the text/drawable/bitmap of your watermark at the location you desire.
Instead of using hardcoded values (50,50) try using calculated values.
canvas.drawText("Watermark",canvas.width/2, canvas.height/2, paint)
This will make the watermark as a part of your image.
If you want to keep it separate but create an illusion of watermark, then you can display your watermark as src in an imageView and overlay it over your actual image display view.
Sorry if I misunderstood. Do share a better solution though!
I am trying to :
Pick a photo from Android gallery (Suppose : person.png)
Cropping the photo as rounded image
Resize the image in 300x300 pixels
Then add the resized image(person.png) on the top of a frame image (frame.png). And the final resulted image will look like this (final_image.png)
Then, save the new image(final_image.png) into gallery with a new name.
person.png
frame.png
final_image.png
Here is my demo code for loading an image in a frame or mask.
ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.content_image);
Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
mImageView.setScaleType(ScaleType.CENTER);
mImageView.setBackgroundResource(R.drawable.background_frame);
I have a Picture object, loaded from an SVG file, and I have set hardwareAccelerated=false to make it works on all devices.
Since there is a bug on android 4.0.4, I have to convert the Picture to Bitmap and I do that, in this way:
...
...
public void draw(Canvas canvas) {
...
...
//myPicture size is 9000x5000 but I want to display only this portion
clipRect.set(50, 50, 370, 530);
Bitmap bmp = getBitmapFromPicture(myPicture, clipRect);
canvas.drawBitmap(bmp, 0, 0, null);
bmp.recycle();
...
...
}
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect) {
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()), Math.round(clipRect.height()), Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture);
}
Now I want to clip the Picture because I want to display only the visible screen part of it.
But the canvas.drawPicture does not accept srcRect parameter.
How is it possible to achieve this?
EDIT:
By translate the canvas: canvas.translate(-50, -50) it seems that translate the bitmap, too.
You need to set a transform on the Canvas.
To move the portion of the picture at 50,50 down so it is on the bitmap (ie. at 0,0), just do:
canvas.translate(-50, -50);
So your method becomes:
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect)
{
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()),
Math.round(clipRect.height()),
Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-clipRect.left, -clipRect.top);
canvas.drawPicture(picture);
}
I have retrieved my profile picture from Facebook and displayed it in the ImageView. But before displaying it I cropped the image to a circular form. The circular image is saved in the sdcard. Now what I want to do is display this image in a square form in another ImageView which is in another activity. So is it possible to change the circular image back to square? If yes please tell me how should I do it. I have cropped the image to circle using the following codes.
Bitmap circleBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader (bm, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bm.getWidth()/2, bm.getHeight()/2, bm.getWidth()/2, paint);
paint.setAntiAlias(true);
paint.setShadowLayer(10, 10, 5,Color.RED);
paint.setShadowLayer(40,100 , 100,Color.RED);
myImageView1.setImageBitmap(circleBitmap);
I am trying to build a Magnify tool in my Android app. For this, I have an ImageView, which I converted to Bitmap (with some zoom/scale factor).
imageView.setDrawingCacheEnabled(true);
Bitmap drawingCache = imageView.getDrawingCache(true);
Matrix matrix = new Matrix();
matrix.postScale(5, 5);
Bitmap viewCapture = Bitmap.createBitmap(drawingCache, 0, 0,
drawingCache.getWidth(),
drawingCache.getHeight(),
matrix, true);
imageView.setDrawingCacheEnabled(false);
Now, I am drawing this Bitmap image "viewCapture" to my canvas. Here, I want only portion of the image to be rendered on the canvas.
I tried using approaches: "setRectToRect() on Matrix", "canvas.drawBitmap(bitmap, src, dst, paint)". But, didn't work out appropriately.
Would using SurfaceViews be helpful? Has anyone come across this situation? Please post your thoughts/ideas.
Why not just use the following?
Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
src corresponds to which region of the bitmap you want to draw and dst says where you want the bitmap drawn. You can read more about it here: http://developer.android.com/reference/android/graphics/Canvas.html
When I do that, I get a smaller image of the bitmap, instead of cropped image. Following is the snippet I have used.
Canvas c = holder.lockCanvas();
c.drawRGB(10, 10, 10);
Rect src = new Rect((int)x - _image.getWidth(), (int) y - _image.getHeight(), (int)x + _image.getWidth(), (int) y + _image.getHeight());
RectF dst = new RectF((int)x - 50, (int) y - 50, (int)x + 50, (int) y + 50);
c.drawBitmap(_image, src, dst, null);
holder.unlockCanvasAndPost(c);