Combining bitmaps and resizing the result - android

I want to combine several smaller tiles/bitmaps into one large bitmap, resize it and then draw it on my canvas.
How should I go about this?

Method for scaling the Bitmap
public Bitmap getImage (int id, int width, int height) {
Bitmap bmp = BitmapFactory.decodeResource( getResources(), id );
Bitmap img = Bitmap.createScaledBitmap( bmp, width, height, true );
bmp.recycle();
return img;
}
Scale the bitmap
Bitmap bitmap = getImage(R.drawable.YOUR_DRAWABLE_HERE, WIDTH, HEIGHT);
Draw the bitmap on your canvas at (x,y) (for more info about Paint click Here)
canvas.drawBitmap(bitmap, x, y, paint);

Related

Combine Images in Canvas and then view it through ImageView using Android Studio

I want to combine images by putting them on top of eachother in Canvas and then view the combination using ImageView. What I know is if you set the first Bitmap into the Canvas everything else you do will be added to that first Bitmap. I only get the error:
PointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
ImageView mainImage = (ImageView) view.findViewById(R.id.mainImage);
Bitmap bottomImage = BitmapFactory.decodeFile("image1.png");
Bitmap topImage = BitmapFactory.decodeFile("image2.png");
Bitmap tempBitmap = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bottomImage, 0, 0, null);
tempCanvas.drawBitmap(topImage, 0, 0, null);
mainImage.setImageBitmap(bitmap);
Try Below Code
public Bitmap drawImageOnImage(Bitmap background, Bitmap pic, int x_to_draw, int y_to_draw, int width, int height)
{
try
{
Canvas newCanvas = new Canvas(background);
Bitmap img_bitmap = Bitmap.createScaledBitmap(pic, width, height, true);
newCanvas.drawBitmap(img_bitmap, x_to_draw, y_to_draw,new Paint());
}catch (Exception e)
{
System.out.println("Error Occured=>");
e.printStackTrace();
}
return background;
}
Here function takes 2 input bitmaps, Background bitmap and another image bitmap to draw over background bitmap. You will get combined bitmap to set for ImageView. X and Y are the co-rdinates where top image bitmap start paint. Same for height and width
After some experiments this code worked perfectly. And I added multiple top images.
private void drawAndView(ImageView imgView, Bitmap bottomImage, Bitmap[] topImages){
Bitmap tempBitmap = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bottomImage, 0, 0, null);
for (Bitmap bitmap : topImages){
tempCanvas.drawBitmap(bitmap, 0, 0, null);
}
imgView.setImageBitmap(tempBitmap);
}

Crop picture from camera by code not by user (Create Bitmap)

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;
}

How to resize bitmap when drawing in canvas?

In my android app, I have this function that creates a new bitmap from an old one via canvas.
private static Bitmap convert(Bitmap bitmap, Bitmap.Config config, int width, int height) {
Bitmap convertedBitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return convertedBitmap;
}
The problem is when I draw the old bitmap onto the canvas, since the old bitmap is bigger in dimensions than the canvas, only the top left part of the bitmap gets drawn on the canvas. Is there a way I can make it so when it draws the bitmap on the canvas, it scales the bitmap so it fits perfectly in the canvas?
I don't want to resize the bitmap using createScaledBitmap. Is there a faster way?
OR Can I do createScaledBitmap but make it mutable at the same time? That is what I am trying to do overall, resize and make mutable at same time.
Thanks
You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) for this function (Android Doc). The code below should accomplish what you are trying to do:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);

Android: Positioning 2 bitmaps on a Canvas

I take picture and obtain a Bitmap (bitmap), when I take the picture there is an image I would like to stay on the top (bitmao2). So to save the picture with the image on it I use a canvas. I would like to position bitmap2 on bitmap exactly how it is positioned when I take the picture.
Here is what I do
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
int width=bitmap.getWidth();
int height=bitmap.getHeight();
Bitmap bitmap2 = GameActivity.decodeSampledBitmapFromResource(gameactivity.getResources(), R.drawable.fire16,width1 );
Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
ImageView image = (ImageView) gameactivity.findViewById(R.id.imageView4);
LayoutParams layout = (LayoutParams) image.getLayoutParams();
int margin = layout.topMargin;
Bitmap cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(cs);
canvas.drawBitmap(bitmap, 0f, 0f, null);
canvas.drawBitmap(bitmap2,0,margin, null);
(I rotate the image because it is in landscape mode even if I take it in portrait mode so I correct that)
The problem is even if dimensions Canvas = dimensions of bitmap, I don't see the entire image after my code is executed, it is a bit cropped and of course bitmap2 is not where it should be.
Maybe I should take the dimensions of the screen instead??
You can use drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint) for drawing bitmaps:
Rect src = new Rect(0, 0, bitmap2.getWidth(), bitmap2.getHeight());
Rect dst = new Rect(0, 0, width, height);
canvas.drawBitmap(bitmap2, src, dst, null);
Also if you rotating your image than width and height will be swapped here:
bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);

how to copy surfaceview to bitmap

i want to copy surfaceview to bitmap , but just get black color (ff000000)
//this.surView is my SurfaceView
Bitmap bitmap =Bitmap.createBitmap(this.surView.getWidth(), this.surView.getHeight(), Bitmap.Config.RGB_565);
//this.can is my canvas
this.can = this.surHolder.lockCanvas(new Rect(0,0,this.surView.getWidth(), this.surView.getHeight()));
this.can.setBitmap(bitmap);
this.surView.draw(this.can);
this.surHolder.unlockCanvasAndPost(can);
// bitmap = Bitmap.createBitmap(bitmap);
//copy bitmap from (srcx,srcy) to (x,y)
bitmap = Bitmap.createBitmap(bitmap, srcx, srcy, cx, cy);
this.ondrawImage(bitmap, x, y);
Use the surfaceview.getDrawingCache() will get Bitmap.

Categories

Resources