4

Is there a way to rotate a bitmap without making a copy of it? Or maybe the imageview that's holding the bitmap? Right now I've got something similar to:

Bitmap bm = BitmapFactory.decodeFile(...
// get the orientation
Matrix m = new Matrix
m.postRotate(orientation)
Bitmap new = Bitmap.createFromBitmap(bm, ..., m);
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
joshkendrick
  • 3,497
  • 8
  • 38
  • 52

1 Answers1

1

There really algorithmically isnt an EASY way of performing this rotation without a completely separate new place to put the rotated copy, then deleting the current (non-rotated) copy. I can think of a potential algorithm where you would essentially have to have one pixel' worth of memory but I would have to spend more time figuring out the actual algorithm.

Also look at this StackOverflow Link: Algorithm to rotate an image 90 degrees in place? (No extra memory)

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • Ok. Well I wasn't too optimistic. I figured if I couldn't find it through google, it probably wasn't available. I suppose I could get the byte array from the bitmap and try to write some method to swap the bytes based on orientation or something... but that just seems like overkill. Thanks anyway – joshkendrick Jun 04 '12 at 17:53
  • There is a method that I have in mind, I just haven't actually ever written it. Basically you would have to move around the image in (essentially) a circular motion with something like a push forward of the pixel values. An example would look like for a 2 by 2: begin at pixel (1,1), copy pixel (1,2), push pixel (1,1)'s value to pixel(1,2). next step copy pixel (2,2), use the copy of pixel(1,2) to push into pixel (2,2). Copy pixel (2,1), push copied pixel (2,2)'s value into pixel (2,1). Finally push copied pixel (2,1)'s value into pixel (1,1). This algo would take 2 pixels worth of space. – trumpetlicks Jun 04 '12 at 18:00
  • Since I'm just copying and swapping ints, it should be pretty fast also I would think. You seem to know a lot more about this than me, so would you know how pixels are organized in the array? I drew you're example out and it seemed the pixels moved around in a circle; fairly simple for 2x2, but if it's say 4x4, do you start at the bottom left and spiral around into the middle? Is that how the bytes are arranged in the array? – joshkendrick Jun 04 '12 at 19:14
  • Not sure really about how android / java arrange their data for this data type. I do know that the provide the following routines for performing pixel manipulation though: getPixels, getPixel, setPixels, and setPixel. You could simply make calls to getPixel, SetPixel to do what you want to perform the algorithm. Here is a link to the Bitmap Type on the Android documentation. http://developer.android.com/reference/android/graphics/Bitmap.html – trumpetlicks Jun 04 '12 at 19:29
  • In terms of the algorithm, YES that was essentially my thought process. Spiral from outside in :-) Again, I havent written the algorithm, but that is the idea. – trumpetlicks Jun 04 '12 at 19:30
  • hmmm alright, well I might give it a shot. Thanks for the help! – joshkendrick Jun 04 '12 at 19:31
  • If you need more help, this is an interesting problem, I will try to help, so just give a shout :-) – trumpetlicks Jun 04 '12 at 19:33