Related
I want to know if it's possible to modify bitmap pixels by pixels. I'm not asking for changing bitmap size/scale. What I'm looking for is that I want to increase size of all or some specific pixels of a bitmap.
I tried this
Thread(Runnable {
val newBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width * 4,
bitmap.height * 4, true);
for (x in 0 until bitmap.getWidth()) {
for (y in 0 until bitmap.getHeight()) {
val pixel = bitmap.getPixel(x, y);
val redValue = Color.red(pixel)
val blueValue = Color.blue(pixel)
val greenValue = Color.green(pixel)
newBitmap.setPixel(x * 4, y * 4, Color.rgb(redValue, greenValue, blueValue))
}
}
runOnUiThread({ imageView.setImageBitmap(newBitmap) })
}).start()
But it has no effect on the bitmap. Any kind of help is highly appreciated.
If you want to pixelate just a part of your bitmap, then you could create a Bitmap of the part which you wish to edit, perform changes on the new bitmap and then overlay the changed bitmap on top of the original bitmap.
So for example, you could pixelate a portion of a Bitmap by doing something along the following lines,
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Bitmap originalBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.drawabledroid)).getBitmap();
// Pixelate just the top half
int x = 0, y = 0, width = originalBitmap.getWidth(), height = originalBitmap.getHeight()/2;
//Or pixelate a rectangle in the middle
//int x = originalBitmap.getWidth()/2 - 50, y = originalBitmap.getHeight()/2 - 50, width = 100, height = 100;
//Get bitmap with region you want to pixelate
Bitmap original = Bitmap.createBitmap(originalBitmap, x,y,width,height);
int normalWidth = original.getWidth(), normalHeight = original.getHeight();
int smallWidth = normalWidth/15, smallHeight = normalHeight/15;
Bitmap small = getResizedBitmap(original, smallWidth, smallHeight);
Bitmap pixelated = getResizedBitmap(small, normalWidth, normalHeight);
//Overlay the pixelated bitmap on top of the original
Bitmap overlayed = overlayBitmaps(originalBitmap, pixelated,x,y, width, height);
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
}
public static Bitmap overlayBitmaps(Bitmap bmp1, Bitmap bmp2, int x, int y, int width, int height) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
//If you know the background in transparent or any specific color
//Then you can color the region before overlaying the pixelated bitmap
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
mPaint.setAntiAlias(true);
canvas.drawRect(x,y, x+width,y+height, mPaint);
//Overlay the pixelated bitmap
canvas.drawBitmap(bmp2, x, y, null);
return bmOverlay;
}
I need to change an image, from circle to normal square.
I'm a beginner a few weeks in programming,
I'm 2 days stopped in this code, does anyone know how to change?
I already tried everything, but I could not solve
I do not understand anything about canvas, I've already studied to solve this problem, but I'm still not able to
public static Bitmap getCircleBitmap(Bitmap bitmap) {
final int scaledWidth = 100;
final int scaledHeight = 100;
bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
Bitmap output;
Rect srcRect, dstRect;
float r;
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
if (width > height) {
output = Bitmap.createBitmap(height, height, Bitmap.Config.ARGB_8888);
int left = (width - height) / 2;
int right = left + height;
srcRect = new Rect(left, 0, right, height);
dstRect = new Rect(0, 0, height, height);
r = height / 2;
} else {
output = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
int top = (height - width) / 2;
int bottom = top + width;
srcRect = new Rect(0, top, width, bottom);
dstRect = new Rect(0, 0, width, width);
r = width / 2;
}
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
bitmap.recycle();
return output;
}
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = AppCompatResources.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
If you look at your code, towards the end of method getCircleBitmap() you have the below line of code:
canvas.drawCircle(r, r, r, paint);
That's the one drawing the circle image for you using DrawCircle.
What you can do is, replace it with DrawRect. That should do that trick for you.
However, if you need to just draw a square image from a vector then you can use a standard ImageView straightaway and use the vector as image resource.
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;
}
I want to scale a Bitmap using Canvas
I need to keep the Bitmap original width and height
and the scaled one aligns center-middle in bound
here is what I have tried:
public Bitmap scale(Bitmap src) {
int width = src.getWidth();
int height = src.getHeight();
float scaledWidth = width * .5f;
float scaledHeight = height * .5f;
Bitmap transBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(transBitmap);
Paint paint = new Paint();
float dx = (width - scaledWidth);
float dy = (height - scaledHeight);
canvas.drawBitmap(src, null, new RectF(dx, dy, scaledWidth, scaledHeight), paint);
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeight), Matrix.ScaleToFit.CENTER);
Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
//return transBitmap;
return output;
}
I used this method in custom ImageView onTouch:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setImageBitmap(scaledBmp);
break;
case MotionEvent.ACTION_UP:
setImageDrawable(originalDrawable);
break;
}
UPDATE
MyImageView.java
public class MyImageView extends ImageView {
private Drawable mDrawable;
private Drawable mScaled;
public MyImageView(Context context, Drawable drawable) {
super(context);
setImageDrawable(drawable);
Bitmap src = ((BitmapDrawable) drawable).getBitmap();
mDrawable = drawable;
mScaled = new BitmapDrawable(getResources(), makeScaled(src));
}
public Bitmap makeScaled(Bitmap src) {
int width = src.getWidth();
int height = src.getHeight();
float scaledWidth = width * .95f;
float scaledHeight = height * .95f;
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeight), Matrix.ScaleToFit.CENTER);
Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
Canvas xfas = new Canvas(output);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
xfas.drawBitmap(output, 0, 0, paint);
return output;
}
#Override public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setImageDrawable(mScaled);
break;
case MotionEvent.ACTION_UP:
setImageDrawable(mDrawable);
break;
}
return super.dispatchTouchEvent(event);
}
}
If I have not misunderstood, one solution is to use a Matrix and its setRectToRect to translate the original values to the desired one. From the documentation
Set the matrix to the scale and translate values that map the source
rectangle to the destination rectangle, returning true if the the
result can be represented.
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeigth), Matrix.ScaleToFit.CENTER);
Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
and then simply draw the scaled bitmap on your canvas
Solved
public static Bitmap scaleBitmap(Bitmap src, float factor) {
int width = src.getWidth();
int height = src.getHeight();
int scaledWidth = (int) (width * factor);
int scaledHeight = (int) (height * factor);
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap scaled = Bitmap.createScaledBitmap(src, scaledWidth, scaledHeight, false);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
int distX = (width - scaledWidth) / 2;
int distY = (height - scaledHeight) / 2;
canvas.drawBitmap(scaled, distX, distY, paint);
return bmp;
}
I needed a demo where any image can be cropped with the face detection function.
FIXED
But after few surfing hours I didn't come to a single demo, so I prepared a single demo with conjunction of few demos that I found online.
I have prepared a demo to crop the image.
My demo crops the image rectangle, and circular as well.
Also it detects the face and crops the image according to the face detection.
I am using the following image to crop it.
And the screenshot of the crop result is :
The xml for the example is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<View
android:id="#+id/part1"
android:layout_width="fill_parent"
android:layout_height="100dp" >
</View>
<View
android:id="#+id/part2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="30dp" >
</View>
</LinearLayout>
The java code for the Activity :
public class MainActivity extends Activity {
public View part1, part2;
int viewHeight, viewWidth;
private FaceDetector myFaceDetect;
private FaceDetector.Face[] myFace;
float myEyesDistance;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
part1 = findViewById(R.id.part1);
part2 = findViewById(R.id.part2);
part1.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
viewHeight = part1.getMeasuredHeight();
viewWidth = part1.getMeasuredWidth();
try {
Paint paint = new Paint();
paint.setFilterBitmap(true);
Bitmap bitmapOrg = BitmapFactory.decodeResource(
getResources(),
R.drawable.sachin_tendulkar_10102013);
int targetWidth = bitmapOrg.getWidth();
int targetHeight = bitmapOrg.getHeight();
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight, Bitmap.Config.ARGB_8888);
RectF rectf = new RectF(0, 0, viewWidth, viewHeight);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRect(rectf, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap(
bitmapOrg,
new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg
.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), paint);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.sachin_tendulkar_10102013,
bitmapFatoryOptions);
myFace = new FaceDetector.Face[5];
myFaceDetect = new FaceDetector(targetWidth, targetHeight,
5);
int numberOfFaceDetected = myFaceDetect.findFaces(
bitmapOrg, myFace);
Bitmap resizedBitmap = null;
if (numberOfFaceDetected > 0) {
PointF myMidPoint = null;
Face face = myFace[0];
myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance();
if (myMidPoint.x + viewWidth > targetWidth) {
while (myMidPoint.x + viewWidth > targetWidth) {
myMidPoint.x--;
}
}
if (myMidPoint.y + viewHeight > targetHeight) {
while (myMidPoint.y + viewHeight > targetHeight) {
myMidPoint.y--;
}
}
resizedBitmap = Bitmap.createBitmap(bitmapOrg,
(int) (myMidPoint.x - myEyesDistance),
(int) (myMidPoint.y - myEyesDistance),
viewWidth, viewHeight, matrix, true);
} else {
resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
viewWidth, viewHeight, matrix, true);
}
/* convert Bitmap to resource */
// Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap,
// 0,
// 0, viewWidth, viewHeight, matrix, true);
BitmapDrawable bd = new BitmapDrawable(resizedBitmap);
part1.setBackgroundDrawable(bd);
} catch (Exception e) {
System.out.println("Error1 : " + e.getMessage()
+ e.toString());
}
}
});
part2.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
viewHeight = part2.getMeasuredHeight();
viewWidth = part2.getMeasuredWidth();
try {
Paint paint = new Paint();
paint.setFilterBitmap(true);
Bitmap bitmapOrg = BitmapFactory.decodeResource(
getResources(),
R.drawable.sachin_tendulkar_10102013);
int targetWidth = bitmapOrg.getWidth();
int targetHeight = bitmapOrg.getHeight();
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight, Bitmap.Config.ARGB_8888);
RectF rectf = new RectF(0, 0, viewWidth, viewHeight);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRect(rectf, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap(
bitmapOrg,
new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg
.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), paint);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.sachin_tendulkar_10102013,
bitmapFatoryOptions);
myFace = new FaceDetector.Face[5];
myFaceDetect = new FaceDetector(targetWidth, targetHeight,
5);
int numberOfFaceDetected = myFaceDetect.findFaces(
bitmapOrg, myFace);
Bitmap resizedBitmap = null;
if (numberOfFaceDetected > 0) {
PointF myMidPoint = null;
Face face = myFace[0];
myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance() + 20;
if (myMidPoint.x + viewWidth > targetWidth) {
while (myMidPoint.x + viewWidth > targetWidth) {
myMidPoint.x--;
}
}
if (myMidPoint.y + viewHeight > targetHeight) {
while (myMidPoint.y + viewHeight > targetHeight) {
myMidPoint.y--;
}
}
resizedBitmap = Bitmap.createBitmap(bitmapOrg,
(int) (myMidPoint.x - myEyesDistance),
(int) (myMidPoint.y - myEyesDistance),
viewWidth, viewHeight, matrix, true);
} else {
resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
viewWidth, viewHeight, matrix, true);
}
/* convert Bitmap to resource */
// Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap,
// 0,
// 0, viewWidth, viewHeight, matrix, true);
BitmapDrawable bd = new BitmapDrawable(resizedBitmap);
part2.setBackgroundDrawable(new BitmapDrawable(
getCroppedBitmap(bd.getBitmap())));
} catch (Exception e) {
System.out.println("Error1 : " + e.getMessage()
+ e.toString());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
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;
int targetWidth = bitmap.getWidth();
int targetHeight = bitmap.getHeight();
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = bitmap;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
}
The demo works for any image put in the drawable folder,
But if you want to crop any dynamic image, for example any image which is downloaded or chosen from gallery, make few changes in the code :
See the line :
Bitmap bitmapOrg = BitmapFactory.decodeResource(
getResources(),
R.drawable.sachin_tendulkar_10102013);
Here I am taking the image from the drawable folder, now for any downloaded image, you just need to save that image in the bitmapOrg variable, so just change the above line twice, one for part1 for rectangle and part2 for ciculart with your downloaded image saving to bitmapOrg as bitmap, and use the demo, it will crop your image in rectangle and circular way.