How to prevent Eraser from Removing Bitmap? - android

How to prevent Eraser from Removing Bitmap or Paint Must Draw in Back Side of Image As per Attach Image & My code is as below in Android Java
In Paint View - onDraw my code is as Below
protected void onDraw(Canvas canvas) {
if (mClear) // clear canvas and reset image stack
{
// mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
// mCanvas = new Canvas(mBitmap);
// mCanvas.drawColor(mBackgroundColor);
// mImages.clear();
// mUndoneImages.clear();
// mImages.push(new Image(mBitmap));
// mPath.reset();
// mClear = false;
// return;
}
if (mEraseMode) // if we want to erase, set color of the brush to background color and width to the value chosen with slider
{
mBrushColor = mEraserColor;
mStrokeWidth = mEraserWidth;
} else // if we exit erase mode go back to old width and color
{
mBrushColor = mOldBrushColor;
mStrokeWidth = mOldStrokeWidth;
}
// draw current path
mPaint.setColor(mBrushColor);
mPaint.setStrokeWidth(mStrokeWidth);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); // draw the main bitmap on canvas
mCanvas.drawPath(mPath, mPaint);
}
I want to Draw Image(Bitmap) Like Below
Here Eraser Should not Remove the Image (Black color Not Remove)
Eraser & Paint Must work like this

Related

How to Auto capture without any user interface ("Button") inside an overlay using camera2 by google?

I am using google camera2 api and was able to successfully make a GREEN rectangular overlay using surface view. Now the challenge is to capture a preview with out button only when the object comes inside rectangular overlay after touching the four corners of the the overlay. Tried alot but still no success. The whole code is from google sample camera2 so presenting only the part where changes were made.
(https://github.com/googlesamples/android-Camera2Basic):
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
view.findViewById(R.id.picture).setOnClickListener(this);
view.findViewById(R.id.info).setOnClickListener(this);
mTextureView = view.findViewById(R.id.texture);
final SurfaceView surfaceView = view.findViewById(R.id.surfaceView);
surfaceView.setVisibility(View.VISIBLE);
surfaceView.setZOrderOnTop(true);
SurfaceHolder mHolder = surfaceView.getHolder();
mHolder.setFormat(PixelFormat.TRANSPARENT);
mHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder)
{
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
int w = canvas.getWidth();
int h = canvas.getHeight();
int outerFillColor = 0x33000000;
float radius = 10.0f;
RectF rect = new RectF(100, 100, w - 100, h - 100);
// first create an off-screen bitmap and its canvas
Bitmap bitmap = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {
bitmap = Bitmap.createBitmap(w, h,
Bitmap.Config.ARGB_8888);
}
Canvas auxCanvas = new Canvas(bitmap);
// then fill the bitmap with the desired outside color
Paint paint = new Paint(Paint.FAKE_BOLD_TEXT_FLAG);
paint.setColor(outerFillColor);
paint.setStyle(Paint.Style.FILL);
auxCanvas.drawPaint(paint);
// then punch a transparent hole in the shape of the rect
paint.setXfermode(new
PorterDuffXfermode(PorterDuff.Mode.CLEAR));
auxCanvas.drawRoundRect(rect, radius, radius, paint);
// then draw the white rect border (being sure to get rid of the xfer
mode!)
paint.setXfermode(null);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
auxCanvas.drawRoundRect(rect, radius, radius, paint);
// finally, draw the whole thing to the original canvas
canvas.drawBitmap(bitmap, 0, 0, paint);
holder.unlockCanvasAndPost(canvas);
}
}
Check this link..
In this section image captures in onClick event. You can capture image without click by placing the capture code where you want
Show rectangle in camera preview and crop image within it using android camera2

Android blur Bitmap Portion(fingure touch portion)/Make certain area of bitmap blur on touch

In one of my app i need to make a bitmap's portion blurred...
I need to allow the user to figure move and make that portion of bitmap blur.
How to apply this??
if it is possible? and if yes then how????
I have visited This site but not fruitful output
How can I do this?Is it possible?
I have done this:
mPaint.setAlpha(10);
// mPaint.setStyle(Style.FILL);
//mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
mPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.INNER));
paint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);
mPaint.setAntiAlias(true);
mPath = new Path();
mPath.offset(30, 30);
paths.add(new PathPoints(mPath, color, false, strokWidth));
mCanvas = new Canvas();
c2 = new Canvas();
// this.setDrawingCacheEnabled(true);
c2.setBitmap(Transparent);
and
protected void onDraw(Canvas canvas1) {
canvas = canvas1;
// === canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawBitmap(Transparent, 0, 0, mBitmapPaint);
c2.drawBitmap(Bitmap2, 0, 0, mBitmapPaint);
// canvas.drawBitmap(Transparent, 0, 0, mBitmapPaint);
// canvas.drawBitmap(Transparent, 0, 0, null);
// c2.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (PathPoints p : paths) {
//mPaint.setColor(p.getColor());
mPaint.setStrokeWidth(p.getStrokWidth());
Log.v("", "Color code : " + p.getColor());
if (p.isTextToDraw()) {
canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
} else {
// if(isTouched)
// {
// canvas.drawPath(p.getPath(), mPaint);
c2.drawPath(p.getPath(), mPaint);
}
}
//
// invalidate();
}
The idea here is not that complex.
You create statically a blurred version of your image (Renderscript maybe?)
then you draw the original image on the canvas.
At this point when the user touches the canvas you draw in the touched area a portion of the blurred bitmap.
To achieve this you might want to try some PorterDuff masking in order to make it look nice (like having an alpha mask to draw the blurred part in order to blend it better with the original image).

To draw transparent line over image in imageview

I have made a paint app in which i have used ImageView in layout to show image that can be taken from camera or gallery.I want to draw transparent line over image so that image can be seen after drawing.please help me.
Thanks for support
I have used the code to make draw line transparent is :
myPaint.setAlpha(50);
My code is:
protected void onDraw(Canvas canvas) {
Toast.makeText(PaintScreen.this, "onDraw is called", Toast.LENGTH_SHORT).show();
// myPaint.setAlpha(100);
canvas.drawBitmap(PaintScreen.this.localBitmap, 0,0,null);
// canvas.drawPath(myPath, paintBlur);
canvas.drawPath(myPath, myPaint); Log.i("OnDRAWING", "REACH ON DRAW"); }
public class CustomView extends ImageView {
private float mX, mY;
public CustomView(Context context) {
super(context);
localBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Config.ARGB_8888);
myCanvas = new Canvas(localBitmap);
myPaint = new Paint(); setPaintForDraw(paintcolor, false, 30);
setFocusable(true);
setFocusableInTouchMode(true); myPath = new Path();
}
}
private void setPaintForDraw(int color, boolean eraseMode, int brushSize) {
//myPaint.setAlpha(100);
myPaint.setAntiAlias(true);
myPaint.setDither(true);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setColor(color);
myPaint.setStrokeCap(Paint.Cap.ROUND);
myPaint.setStrokeJoin(Paint.Join.ROUND);
myPaint.setStrokeWidth(brushSize);
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
if (eraseMode) {
myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
else { myPaint.setXfermode(null); }
}
see this thread How to maintain multi layers of ImageViews and keep their aspect ratio based on the largest one?, here you can use multiple Draeable layers that are drawn over the image
First, you have to check that your Bitmap is mutable. If it is not, make a copy of it. And here is how you can draw a line on your image:
Bitmap copyBmp = yourBMP.copy(Bitmap.Config.ARGB_8888, true); //Copy if yourBMP is not mutable
Canvas canvas = new Canvas(copyBmp);
Paint paint = new Paint();
paint.setAlpha(50); //Put a value between 0 and 255
paint.setColor(Color.GRAY); //Put your line color
paint.setStrokeWidth(5); //Choose the width of your line
canvas.drawLine(startX, startY, stopX, stopY, paint); //Set the coordinates of the line
Now, if you display copyBmp, you should see a line drawn over it.

How to work with finger eraser in android?

I have prepared one paint app.In my app we can draw any thing.It is working fine.Here i want prepare finger erase for erase paint.Eraser is working,but it is eraser all the drawn paint.I want to eraser only where i touch if drawn paint is there for that i wrote some code,
this my ondraw method,
public void onDraw(Canvas canvas) {
if (myDrawBitmap == null) {
myDrawBitmap = Bitmap.createBitmap(480, 800,
Bitmap.Config.ARGB_8888);
mBmpDrawCanvas = new Canvas(myDrawBitmap);
mIntDrawArray = new int[myDrawBitmap.getWidth()
* myDrawBitmap.getHeight()];
}
if (mBmpDrawCanvas != null) {
myDrawBitmap.getPixels(mIntDrawArray, 0, myDrawBitmap.getWidth(),
0, 0, myDrawBitmap.getWidth(), myDrawBitmap.getHeight());
for (Path path : ILearnPaintActivity.mArryLstPath) {
if (ILearnPaintActivity.mArryLstPath.contains(path)
&& ILearnPaintActivity.paintAndEraserFlag == 1) {
mPaint.setXfermode(new PorterDuffXfermode(
PorterDuff.Mode.CLEAR));
mBmpDrawCanvas.drawPath(ILearnPaintActivity.mPath, mPaint);
} else {
mBmpDrawCanvas.drawPath(ILearnPaintActivity.mPath, mPaint);
}
}
if (myDrawBitmap != null)
canvas.drawBitmap(myDrawBitmap, 0, 0, null);
}
}
draw paint is working fine.In same activity i have one button "Eraser". when we click on eraser button i assign flag for difference.Please help me how to do this...
first u need to make clear what erase.
for vector base canvas it's delete vector element.
for pixel base canvas it's mean draw with backgournd color. (or make it's transparency)
so in my point of view. when erase. you can change a Paint with backgournd color. and continue draw a very bold line on bitmap by touch.
try like this
mBitmap.eraseColor(Color.TRANSPARENT); // Bitmap erase color
mPath.reset(); // your path
mView.invalidate(); // your View Path

Why does my bitmap on the canvas appear below the background drawable?

I have a SurfaceView where I'm setting a background color and an image like so:
BitmapDrawable tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);
tiledBackground.setColorFilter(0xaacceeff, PorterDuff.Mode.DST_OVER);
this.setBackgroundDrawable(tiledBackground);
I also have an animation thread where I'm drawing an image (successively adjusting its x coordinate so that it appears to move to the left). The background image is a transparent PNG and so some parts of it are transparent. It appears that the image I'm drawing from the thread appears below the background drawable on the SurfaceView. How can I have it appear on top of the background? I'm drawing the image like so:
private void doDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(missile, x, getHeight() - 95, paint);
canvas.restore();
}
missile and paint are initialized in the constructor of the thread to:
missile = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.missile));
paint = new Paint();
Each call to doDraw should draw everything you want to be displayed, including the background.
// Add to initializer
tiledBackground = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background));
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);
tiledBackground.setColorFilter(0xaacceeff, PorterDuff.Mode.DST_OVER);
private void doDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
// Create a rectangle (just holds top/bottom/left/right info)
Rect drawRect = new Rect();
// Populate the rectangle that we just created with the drawing area of the canvas.
canvas.getClipBounds(drawRect);
// Make the height of the background drawing area equal to the height of the background bitmap
drawRect.bottom = drawRect.top + tiledBackground.getBitmap().getHeight();
// Set the drawing area for the background.
tiledBackground.setBounds(drawRect);
tiledBackground.draw(canvas);
canvas.drawBitmap(missile, x, getHeight() - 95, paint);
canvas.restore();
}

Categories

Resources