how to copy surfaceview to bitmap - android

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.

Related

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

Android Image Masking with Rotate & Scale

How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);
Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);
original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);
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.FIT_XY);
mImageView.setBackgroundResource(R.drawable.background_drawable);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again

Combining bitmaps and resizing the result

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

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

Rotating a drawable in Android

How can a Drawable loaded from a resource be rotated when it is drawn? For example, I would like to draw an arrow and be able to rotate it to face in different directions when it is drawn?
You need to use Bitmap and Canvas Class functions to prepare drawable:
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
mImageView.setImageBitmap(bmResult);
In this code sample rotation for 90 degrees over image center occurs.
essentially it can be boiled down to: do a(n inverse) canvas transformation instead of transforming drawable
private BitmapDrawable drawable; // or Drawable
protected void onDraw(Canvas canvas) { // inherited from View
//...
canvas.save();
canvas.rotate(degrees, pivotX, pivotY);
drawable.draw(canvas);
canvas.restore();
//...
}
if you have BitmapDrawable it may be desirable to increase quality of the output by setting antialiasing
drawable.setAntialias(true);
Accepted answer doesn't work for me. I have non square image, so I changed his code a bit.
private Bitmap rotateDrawable(#DrawableRes int resId) {
Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), resId);
Bitmap bmpResult = Bitmap.createBitmap(bmpOriginal.getHeight(), bmpOriginal.getWidth(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmpResult);
int pivot = bmpOriginal.getHeight() / 2;
tempCanvas.rotate(90, pivot, pivot);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
return bmpResult;
}
mImageView.setImageBitmap(rotateDrawable(R.drawable.some_image));
essentially this:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
source link:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#tween-animation

Categories

Resources