Mirror app is not that simple

A couple of months ago I published my new app Dr Booger. Let me tell you, it was not easy.

It’s a mirror app that uses the camera and zooms in to the maximum point that your front facing camera can handle.

It actually took a couple of hours to get the camera to start and zoom in to the maximum point. However the problem was to stop the app from crashing every time I minimized it. I had to figure out how to handle a release of the camera when I stopped using it. That took a couple of weeks…

According to the documentation, you have to release the camera in the onPause() life cycle when you switch to another app or when you click on the take a picture button, and start it over again in the onResume() when you go back to the app where the surface view displays the camera screen. That was the hardest thing to get working properly, depending on the android version and depending on the different phones that I was testing my app on. There are some phones that handle the camera differently, so you have to keep that in mind when you call the release() function.

“10. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume())”.

For Some reason that I have not yet figured out, when I released the camera in the onPause() it crashed. For me It was better to call release in the onStop() lifecycle instead of the onPause(). I imagine it has to do with the way I set up my code.

Some of you might wonder why I did not just use the many API’s that were out there that handle all that stuff. You will find many examples online. For learning purposes, I wanted to use the bare bones camera hardware in my app without using the phone’s native camera application which would have been a faster way of doing it.

Take for example Vogella’s great tutorial, which utilizes the Camera via the integration of the phone’s camera, by starting the existing Camera application with an intent.

I preferred the approach of directly integrating the camera into my app via the Camera API provided in the API guides for android developers. And it was indeed a learning experience.

Keep in mind that the Camera class is actually deprecated and google recommends using the new camera2 class. I however decided to use the deprecated camera class because I found that the examples provided were very good and thorough.


Follow these 2 links if you want to jump straight to using the camera2 class.

Detecting camera features with Camera2 and android-Camera2Basic.

I used extensively the following android documentation to create my app:

Camera

Camera API

Controlling the Camera

Good Luck!

Comments

Popular Posts