Tuesday, September 03, 2013

rotate image in android apps

i guess i'm starting to learn a thing or two about organizing my stuff, and as result decided to post the result of one task that is a part of a bigger task, that is a part of an idea that i'm already getting tired of chasing...

my app has to display images and i want to be able to rotate them! sounds simple, and it is! especially with the miracle of Internet, GOOG and stackoverflow.com

so we have this imageview...
  ImageView imageView = (ImageView) findViewById(R.id.test_image1);

we're loading some picture onto it...
 Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

and turns out there is this lovely Matrix class, that exists for all kind of math abuses
                 Matrix mat = new Matrix();
                 mat.postRotate(gradus);


so we can take the original bitmap and apply the matrix operation on it
                  Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);

and when we set this image on the imageView we expect to see the rotated image, right?
                imageView.setImageBitmap(bMapRotate);

but somewhere something is wrong or some manual has not been read, cause a vital piece is missing: you have to set scale type on the imageView, (before assigning the new image?)
              imageView.setScaleType(ScaleType.MATRIX);

now this will work fine and will leave this highly uninformative post online, so i can remember how i did it!

i simply don't have the time to research the why's and do's - at least not right now that i'm still in prototyping phase and the steam has already began to ...steam away...
i lost a few days (all right - couple of hours over few days) with the rotate images problem and at least it made me learn to split bigger tasks into smaller ones - and how effective and helpful it can be!

Monday, August 26, 2013

android camera.open() freeze

weirdest thing happened while i was experimenting with android.hardware.camera api.
i was testing on a 2.2 device and of course there were the problems with
Camera.getNumberOfCameras() missing etc, but when i got to the point to get a handle to the camera with a simple camera.open() - nothing happened!

literally, because the app froze and nothing would unfroze it. it even blocked other applications trying to use the camera, and that until reboot of the device.

i thought maybe it happened because i was on the ui thread, but even as i pushed the camera code to an AsyncTask,doInBackground(), while the ui thread was responsive there was never response from the async portion

weird, a?

i read this was only fixed about API 5 releases and 3 years later. wow!

the obvious workaround is to use intent as i was originally planning, but is this really such a big, unsolvable problem? certainly there are apps that are using the hardware.camera api so how do they manage to workaround it? or maybe the problem is gone in the camera.open(int) method, which is introduced in 2.3

in any case, android is fun. i wonder if you'd agree with this statement if you we're working on it for money under strict client requirements...

UPDATE 2013/09/06:

So there is a way to get handle of the camera, as i suspected in involves SurfaceView which i didn't want to get much into at the time of the previous writing as i was planning to use intent, but actually there are many examples for using SurfaceView out there and here is the code that worked for me:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        surface_view = new SurfaceView(getApplicationContext());
        addContentView(surface_view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        if (surface_holder == null) {
            surface_holder = surface_view.getHolder();
            surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//http://stackoverflow.com/a/6793598/1419136
        }

        sh_callback = my_callback();
        surface_holder.addCallback(sh_callback);
    }
   
    SurfaceHolder.Callback my_callback() {     
        SurfaceHolder.Callback ob1 = new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                  mCamera.stopPreview();
                  mCamera.release();
                  mCamera = null;
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mCamera = Camera.open();

                  try {
                       mCamera.setPreviewDisplay(holder); 
                  } catch (IOException exception) { 
                        mCamera.release(); 
                        mCamera = null; 
                  }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                    int height) {
                mCamera.startPreview();
            }
        };
        return ob1;
}

this is all from the wonderful post at http://stackoverflow.com/a/10482872/1419136 by Bharat Sharma

a very nice example of actually taking a picture is here: http://stackoverflow.com/a/16399425/1419136, by user jiahao

so stackoverflow has been getting huge over the view years, i still remember one of its founders writing a post about dreaming about such a hub of help for developers/admins and all kinds of people looking for qualified answers - well done!

Tuesday, January 01, 2013

hello github

i posted the code for the android app i mentioned earlier in github here: https://github.com/esdee-git/missed-calls

since the app itself was my coding test for android platform, a good decision was to use it as test of github :)

must say that github's windows app is pretty cool, although i thought it was capable to do more - tortoiseSvn style maybe? still checking things around so it may turn more powerful yet, for now i'll do mostly with the git console

grasping git easier as i have svn experience, but i realise the two differ a lot, but i guess it is mostly on a higher/architecture level

in any case, it would be nice if someone found the code useful, and maybe even make it better

enjoy and a Happy New Year

2013 yeyyyy!