Weird behaviour canvas - Bitmap rotated by itself - android

I'm creating a view that will allow me to draw over a given bitmap.
As I create the canvas I set the bitmap as the base for it. It works perfectly on an emulator but, when I try to use it on my phone it behave strangely and rotate the bitmap by 90 degrees.
Here is how I create the canvas. I get the right backgroundPicture from my previous activity.
mBitmap = backgroundPicture.copy(Bitmap.Config.ARGB_8888, true);
mBitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
mCanvas = new Canvas(mBitmap);
ANd here are the screenshots
When I take the picture :
What it displays in my recycler view (displays normally)
What it displays on the canvas (90 degrees on the left)
Thank you for your help, I'm not used to work with canvases so I'm a bit lost :/

After some digging it appears that the problem comes from my smartphone, a Samsung Galaxy A3. In some Samsung models the camera is rotated to gain place for the others components of the phone. Some Samsung handle the difference but if they don't you'll get an rotation of 0 and you'll have to handle the rotation problem model by model manually.
I hope this will help other people.
Cordially,
Matthieu

Related

Drawing application with resize when orientation is changed

I'm sorry to disturb, but at this point, i'm really stuck.
I'm trying to make an activity to draw on a picture. But i've no idea how to handle the rotation of the device.
If i change the bitmap with createScaledBitmap, that works but the draws are not in the right place. (The bitmap is bigger and added space)
I need to stretch the bitmap for the new size (in a way that draws are on the right place on the picture).
Everytime my configuration change, i do that :
snapshotCanvas.mTempBitmap = Bitmap.createScaledBitmap(snapshotCanvas.mOriginalBitmap.copy(snapshotCanvas.mOriginalBitmap.getConfig(), true), drawView1.getWidth(), drawView1.getHeight(), false);
snapshotCanvas.mCanvas.setBitmap(snapshotCanvas.mTempBitmap);
snapshotCanvas.mCanvas.scale((float) snapshotCanvas.mCanvas.getWidth() / drawView.getWidth(),
(float) snapshotCanvas.mCanvas.getHeight() / drawView.getHeight());
I tried many things but i got terribly lost.
Thanks in advance.

Use an image as the background of a canvas and keep it resized

I'm currently working on an Android app. I have a canvas onto which the user will draw something. I need to have a picture as the background of this canvas.
I already managed to import a pic and set it as the background using
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_background_pic));
However, the picture appears ten times bigger than the screen.
Is it possible to keep the picture resized, so it fills the canvas and not more?
Also, later I will need to add other things to the activity. The canvas will not be the only thing on the screen, so I need the picture to be the size of the canvas, and not the screen.
Thanks a lot.
Nevermind I found it:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);
By Kai at stackoverflow.com/a/27466127/8205282

Android canvas rotate calculation

I'm creating a live wallpaper that I want a Picture to cover the entire canvas so I draw it like this when in landscape mode
Rectangle r = new Rect(0, canvas.getHeight()- (canvas.getHeight()-statusBarHeight), canvas.getWidth(), canvas.getHeight());
c.drawPicture(pictureBackground, r);
Notice I have to take into consideration the status bar. Okay, that works fine. Now what I want to do is rotate the canvas 90 degrees for portrait mode (btw I can't just rotate the picture outside of code, have to do it this way) so I do this:
canvas.rotate(90,canvas.getWidth()/2,canvas.getHeight()/2);
Now what rectangle should I use to cover the canvas in its entirety when I go to draw the picture again (remember I have to account for the status bar)????
Rectangle r = new Rect(?,?,?,?)
I can't figure it out, I've tried so many possible combinations

rotating and cropping a bitmap accurately and effiently in android

I'm writing an android application.By using it,a user can crop the bitmap image.But I want to add more function,that is to rotate the image before cropping.
I followed the below steps to accomplish this purpose.
built a custom view.
initialized BitmapDrawable() and a cropping rectangle(Rect()) [the rectangle determines where to cop in the bitmap]
override onTouch(MotionEvent) method for two functions
a. user can move the bitmap over the screen
b. user can transform the cropping rectangle
override onDraw(Canvas) method to update the screen.This code is for showing bitmap.
bitmap_drawable.draw(canvas,_bitmap_paint);
Problem:1
This step ,that I don't know how to do, is to show a rotated properly. I used this code to rotate the canvas in the onDraw(Canvas) method.
canvas.rotate(_angle);
But it makes all the things on the screen (including the cropping rectangle) to rotate.
Question:2
Is it a wise way to initialize my own Canvas() object to draw the bitmap on?
Note:
Here,the rotation that I want,is very accurate up to 1 degree and is performed by two-finger rotation gesture so that performance is very important and the following code(using matrix) is bad.
Matrix mat = new Matrix();
mat.preRotate(_degree, _one_finger_x, _one_finger_y);
Bitmap _new_bitmap = Bitmap.createBitmap(_old_bitmap, 0, 0, bmWidth, bmHeight, mat, true);
Therefore,I think initializing a separate Canvas() and using canvas.rotate(_degree) is very appropriate.However,this makes controlling touch events quite difficult.Any ideas?Please help.
Problem:2
Suppose that I can now rotate the bitmap separately from all objects,i.e. the cropping rectangle,etc.This step is to crop the bitmap and show the cropped result to the user efficiently.When rotate function is not included, I simply copy the color bytes within the cropping rectangle on the old bitmap as follow.
Bitmap _new_bitmap = Bitmap.createBitmap( _old_bitmap, _cropRectx, _cropRecty, _cropRectWidth, _cropRectHeight, null);
But here,from a rotated bitmap, how can I clone the color bytes through a crossing crop rectangle?
Question:2
How can I crop a rotated bitmap?

android.graphics.camera Error

I am developing an Android app on eclipse and have been using android.graphics.camera to 3d transform images. In a nutshell, my app takes a 2d image and gives it perspective. My problem is that I also want to rotate the image around the screen while it is being transformed.
final Matrix mMatrix = canvas.getMatrix();
canvas.save();
mCamera.save();
mCamera.rotateX(60);
mCamera.rotateY(0);
mCamera.rotateZ(0);
mCamera.getMatrix(mMatrix);
mMatrix.preTranslate(-this.gridWidth / 2, -this.gridHeight);
mMatrix.postTranslate(this.gridWidth / 2, this.gridHeight);
canvas.concat(mMatrix);
mCamera.restore();
//Draw and move image here
canvas.restore();
When the image gets to the bottom of the screen, where the camera is translated, the image becomes distorted. I see pieces of it on the screen but its like its being drawn backwards or sideways. I've also tried rotating the image using the rotateZ property but the same effect occurs. Once the image gets 'behind' the translation point, it just goes nuts.
I thought it might be an emulator bug so I loaded it on my Droid X and Acer Iconia and the effect remains the same.
I haven't seen anybody else have this problem so i figured someone here might have a clue as to what's going on.
Any Ideas?
This issue can be addressed with newer versions of Android that let you change the camera distance:
http://developer.android.com/reference/android/graphics/Camera.html#setLocation(float, float, float)

Categories

Resources