change part of image color - android

in my app, i want to change text color in below image, when user clicked each word , i have position of each words in data base and draw a rectangle with canvas.drawRect but i want to change exactly color of texts in my ImageView:
my code in Imageview is :
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#5DFFF700"));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if (highlight) {
float rightx = thisL.maxX/scale;
float leftx = thisL.minX/scale;
float bottomy = thisL.maxY/scale;
float topy= thisL.minY/scale;
canvas.drawRect(leftx, topy, rightx, bottomy, paint);
}
//slctd word
if (slctdWord!=null) {
paint.setStyle(Paint.Style.FILL_AND_STROKE);
float wrightx = slctdWord.max_x / scale;
float wleftx = slctdWord.min_x / scale;
float wbottomy = slctdWord.max_y / scale;
float wtopy = slctdWord.min_y / scale;
canvas.drawRect(wleftx, wtopy, wrightx, wbottomy, paint);
}
}

at last i crop a part of my imageview,change color it and draw this with canvas:
(onDraw)
Bitmap bm=((BitmapDrawable)getDrawable()).getBitmap();
Bitmap crdb = changeBitmapColor(Bitmap.createBitmap(bm,
Math.round(leftx), Math.round(topy), Math.round(rightx-leftx),Math.round(bottomy-topy)));
canvas.drawBitmap(crdb,leftx,topy,null);
method changeBitmapColor
public Bitmap changeBitmapColor(Bitmap sourceBitmap)
{
Bitmap resultBitmap = sourceBitmap.copy(sourceBitmap.getConfig(),true);
Paint paint = new Paint();
ColorFilter filter = new LightingColorFilter(Color.WHITE, Color.RED);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
return resultBitmap;
}

Related

Is there a way to achieve a stroke-like effect on bitmaps?

I would like to achieve on a bitmap an effect equivalent to what
paint.setStyle(Paint.Style.STROKE)
canvas.drawText(string, x, y, paint);
does for text.
Something akin to BlurMaskFilter, but for stroke, not glow, nor shadow.
Or if there isn't a built-in way, perhaps someone can suggest an algorithm to achieve this?
private Bitmap multiplyAlpha(final Bitmap bitmap, Paint paint,
final boolean color, final float x) {
paint = new Paint(paint.getFlags());
Bitmap result = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), color ? Config.ARGB_8888 : Config.ALPHA_8);
// ColorMatrixColorFilter requires ARGB.
Bitmap auxiliary = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
new Canvas(auxiliary).drawBitmap(bitmap, 0, 0, paint);
// #formatter:off
paint.setColorFilter(new ColorMatrixColorFilter(new float[] {
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, x, 0
}));
// #formatter:on
new Canvas(result).drawBitmap(auxiliary, 0, 0, paint);
return result;
}
private Bitmap opaque(final Bitmap bitmap, Paint paint, final boolean color) {
return multiplyAlpha(bitmap, paint, color, 255);
}
private RectF inset(final RectF rectF, final float dx, final float dy) {
RectF result = new RectF(rectF);
result.inset(dx, dy);
return result;
}
private RectF offset(final RectF rectF, final float dx, final float dy) {
RectF result = new RectF(rectF);
result.offset(dx, dy);
return result;
}
private Bitmap antialias(final Bitmap bitmap, Paint paint,
final float radius) {
Bitmap result = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ALPHA_8);
if (radius > 0) {
paint = new Paint(paint.getFlags());
Canvas canvas = new Canvas(result);
Bitmap opaque = opaque(bitmap, paint, false);
canvas.drawBitmap(opaque, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
paint.setMaskFilter(new BlurMaskFilter(radius, Blur.INNER));
canvas.drawBitmap(opaque, 0, 0, paint);
}
return result;
}
private Bitmap stroke(final Bitmap bitmap, Paint paint, final float radius,
final RectF rectF, final float dx, final float dy) {
paint = new Paint(paint.getFlags());
Bitmap result = Bitmap.createBitmap(
(int) Math.ceil(rectF.width() + 2 * radius),
(int) Math.ceil(rectF.height() + 2 * radius), Config.ALPHA_8);
if (radius > 0) {
paint.setMaskFilter(new BlurMaskFilter(radius, Blur.NORMAL));
new Canvas(result).drawBitmap(opaque(bitmap, paint, false), null,
offset(rectF, -dx, -dy), paint);
}
return result;
}
private Bitmap stroke(final Bitmap bitmap, Paint paint, final float radius,
final RectF rectF, final int color, final float antialias,
final float factor, final boolean fill, final float dx,
final float dy) {
paint = new Paint(paint.getFlags());
Canvas canvas = new Canvas();
paint.setColor(color);
Bitmap stroke = stroke(bitmap, paint, radius, rectF, dx, dy);
Bitmap auxiliary = Bitmap.createBitmap(stroke.getWidth(),
stroke.getHeight(), Config.ALPHA_8);
canvas.setBitmap(auxiliary);
// Paint [opaque] stroke.
canvas.drawBitmap(opaque(stroke, paint, false), 0, 0, paint);
// Antialias stroke with outside.
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Bitmap outer = multiplyAlpha(antialias(stroke, paint, antialias),
paint, false, factor);
canvas.drawBitmap(outer, 0, 0, paint);
paint.setXfermode(null);
// If FILL, leave the inside filled with color (this is the way e.g.
// Photoshop strokes); otherwise, the stroke will be only on the outside
// of the bitmap; the more transparent the bitmap, the more noticeable
// the effect.
if (!fill) {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawBitmap(opaque(bitmap, paint, false), null,
offset(rectF, -dx, -dy), paint);
paint.setXfermode(null);
}
Bitmap result = Bitmap.createBitmap(auxiliary.getWidth(),
auxiliary.getHeight(), bitmap.getConfig());
canvas.setBitmap(result);
RectF output = offset(rectF, -dx, -dy);
canvas.drawBitmap(auxiliary, null, inset(output, -radius, -radius),
paint);
// Paint bitmap.
canvas.drawBitmap(bitmap, null, output, paint);
// Antialias stroke with bitmap.
Bitmap inner = multiplyAlpha(antialias(bitmap, paint, antialias),
paint, false, factor);
canvas.drawBitmap(inner, null, output, paint);
return result;
}
private void stroke(final Bitmap bitmap, Paint paint, final float radius,
final RectF rectF, final int color, final float antialias,
final float factor, final boolean fill, Canvas canvas) {
float dx = rectF.left - radius;
float dy = rectF.top - radius;
Bitmap stroke = stroke(bitmap, paint, radius, rectF, color, antialias,
factor, fill, dx, dy);
canvas.drawBitmap(stroke, dx, dy, paint);
}
where:
antialias is the width of the antialias effect
factor modifies the antialias strength
fill indicates whether to false paint only the stroke outside, or true also fill the inside (this is the way e.g. Photoshop strokes); the more transparent the bitmap, the more noticeable the effect
rectF indicates the RectF to draw in
usage (e.g.):
stroke(bmp, paint, radius, rectF, 0xffff0000, antialias, factor, fill, canvas);

Are there any libraries that support Android images to add watermarks?

I tried for a long time and couldn't get any better,On different mobile phones:
1. I would like to add pictures and words to the sourceBitmap.
2. want to be able to adjust bitmap and word positions.
Without using library simply we can watermark image using canvas and paint concept
Point point=new Point();
point.set(180, 1000);
Bitmap b=waterMark(BitmapFactory.decodeResource(getResources(), R.drawable.image),"your Text",point,Color.WHITE,90,30,true);
imageView.setImageBitmap(b);
the method code
public Bitmap waterMark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline) {
//get source image width and height
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
//create canvas object
Canvas canvas = new Canvas(result);
//draw bitmap on canvas
canvas.drawBitmap(src, 0, 0, null);
//create paint object
Paint paint = new Paint();
//apply color
paint.setColor(color);
//set transparency
paint.setAlpha(alpha);
//set text size
paint.setTextSize(size);
paint.setAntiAlias(true);
//set should be underlined or not
paint.setUnderlineText(underline);
//draw text on given location
canvas.drawText(watermark, location.x, location.y, paint);
return result;
}
This method will automatically adjust the size of the watermark text according to the size of bitmap. Additionally it will also add the watermark diagonally if the image is portrait and horizontal if image is landscape
public static Bitmap waterMark(Bitmap src, String watermark, int color, int alpha, int size) {
//get source image width and height
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
//create canvas object
Canvas canvas = new Canvas(result);
//draw bitmap on canvas
canvas.drawBitmap(src, 0, 0, null);
//create paint object
Paint paint = new Paint();
//apply color
paint.setColor(color);
//set transparency
paint.setAlpha(alpha);
//set text size
paint.setTextSize(size);
paint.setAntiAlias(true);
Paint stkPaint = new Paint();
stkPaint.setStyle(Paint.Style.STROKE);
stkPaint.setStrokeWidth(1);
stkPaint.setTextSize(size);
stkPaint.setAlpha(alpha);
stkPaint.setColor(Color.BLACK);
stkPaint.setAntiAlias(true);
//set should be underlined or not
//draw text on given location
canvas.save();
float width = paint.measureText(watermark);
Logger.e("width of bitmap", "" + w);
Logger.e("width of text", "" + width);
if (width > w) {
int i = 2;
while (width > w) {
paint.setTextSize(size - i);
stkPaint.setTextSize(size - i);
width = paint.measureText(watermark);
Logger.e("width of text", "" + width);
i++;
}
} else {
int i = 2;
while (width < w) {
paint.setTextSize(size + i);
stkPaint.setTextSize(size + i);
width = paint.measureText(watermark);
Logger.e("width of text", "" + width);
i++;
}
}
if (w < h) {
canvas.rotate(-60, src.getWidth() / 2, src.getHeight() / 2);
}
float final_x = (src.getWidth() - width) / 2;
Point p = new Point();
p.set((int) final_x, src.getHeight() / 2);
canvas.drawText(watermark, p.x, p.y, paint);
canvas.drawText(watermark, p.x, p.y, stkPaint);
canvas.restore();
return result;
}

How to get particular cropping image using canvas circular image?

I am create a custom ImageView with Pinch IN-OUT zoom and circular crop image.The Pinch in-out is wroking fine but when i trying to cropping image, can't get the particulr circle image. I'm using Pinch in-out working based on onTuchListener and Circular Cropping based on canvas class.
I have used below mentioned code for Pinch in-out and Circle Crop image:
#Override
protected void onDraw(Canvas canvas) {
onDrawReady = true;
imageRenderedAtLeastOnce = true;
if (delayedZoomVariables != null) {
setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType);
delayedZoomVariables = null;
}
super.onDraw(canvas);
if (bitmap == null) {
circleWindowFrame(); //Creating circle view
}
canvas.drawBitmap(bitmap, 0, 0, null);
}
protected void circleWindowFrame() {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas osCanvas = new Canvas(bitmap);
RectF outerRectangle = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(getResources().getColor(R.color.overlay));
paint.setAlpha(99);
osCanvas.drawRect(outerRectangle, paint);
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
float centerX = getWidth() / 2;
float centerY = getHeight() / 2;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
float radius = width / 2;
osCanvas.drawCircle(centerX, centerY, radius, paint);
}
This code for Cropping:
public static Bitmap getCrop() {
Bitmap circleBitmap;
circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
return bitmap;
}
Thanks for Advance...
try this
public static Bitmap toOvalBitmap(#NonNull Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
RectF rect = new RectF(0, 0, width, height);
canvas.drawOval(rect, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return output;
}

How to add String on Bitmap Image in Android?

I want to add a string on bitmap image.I have a metod drawTextToBitmap,this method working success place string on bitmap image.But my bitmap image is very small like pinmark image.This function set the string based on the bitmap height and width.I want to place the string exceed than the bitmap image.So Please help me to solve the problem.
Following method i am using to get bitmap :
public Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if (bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.BLACK);
// text size in pixels
paint.setTextSize((int) (70 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int m = (bitmap.getWidth() - bounds.width()) / 2;
int l = (bitmap.getHeight() + bounds.height()) / 2;
canvas.drawText(gText, 1000, l, paint);
return bitmap;
}
Try this:
public static Bitmap drawStringonBitmap(Bitmap src, String string, Point location, int color, int alpha, int size, boolean underline,int width ,int height) {
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(string, location.x, location.y, paint);
return result;
}

Drawing 2 circles on a canvas

I'm trying to draw two circles like this:
This is how I'm trying to do it:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(bmp);
RectF rect = new RectF(0,0,width,width);
Paint paint = new Paint();
drawCircles(paint, c, width, height, width);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
img.setScaleType(ScaleType.FIT_CENTER);
And here is my drawCircles() method:
private void drawCircles(Paint paint, Canvas c, int width, int height, int radius) {
paint.setARGB(255, 255 , 10, 21);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.BUTT);
paint.setStyle(Paint.Style.STROKE);
if(width < height && radius == 0){
radius = width/2;
height = width;
} else if (radius == 0){
radius = height/2;
width = height;
}
Paint paint2 = new Paint();
paint2.setARGB(255, 255 , 10, 21);
paint2.setStrokeWidth(10);
paint2.setAntiAlias(true);
paint2.setStrokeCap(Paint.Cap.BUTT);
paint2.setStyle(Paint.Style.STROKE);
c.drawCircle(width/2, height/2, radius-10, paint);
c.drawCircle(width/2, height/2, 50, paint2);
}
I don't know why but I get only one circle, the small one (the one drawn with paint2).
What can be the reason?
Try this code.Hope it may helps :)
public class SimpleCircleActivity extends Activity
{
private CircleDemoView circledemoView ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
circledemoView =new CircleDemoView(this);
setContentView(circledemoView);
}
private class CircleDemoView extends View
{
public CircleDemoView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);
p.setPathEffect(dashPath);
p.setStyle(Style.STROKE);
for (int i = 0; i < 2; i ++) {
canvas.drawCircle(200, 200, 50+(i*40), p);
}
invalidate();
}
}
}

Categories

Resources