Wednesday, November 18, 2015

Doing Battle With, and Losing To, the Android Camera2 API

I'm back to fiddling with with my Android Bluetooth Thermal Printer. Specifically, I'm building a small camera app that outputs to the printer directly, rather than saving files on your phone.

Looking at the Camera API, I realized that it was now deprecated, so I started building the app using Camera2. I'm not proud to admit it, but Camera2 beat me. I give up. That's one complete and crazy API to use. That's not a bad thing, it's just not ideal for quick little apps.

Oh, and all of this is written in Kawa Scheme. So if you're looking for Scheme Android examples, or how you might invoke the camera from a Scheme app, check out my project's repository, hopefully it'll be of some use.

For now, the app does little more than open up the camera, switch into preview mode and focus when you press the screen. But hopefully with the Camera functionality in place, hooking in my existing Thermal Printer Code shouldn't be too painful.

Two gotchas that I overcame:

1. I was getting the message: A TextureView or a subclass can only be used with hardware acceleration enabled, and of course the camera didn't work. I tried turning on Hardware Acceleration in the AndroidManifest.xml. That made no difference. Ultimately, it was this note that solved the issue:

The default value is "true" if you've set either minSdkVersion or targetSdkVersion to "14" or higher; otherwise, it's "false".

Turns out, I wasn't setting minSdkVersion at all. I did so (setting it to 22), and that fixed the issue.

2. I fired up the camera preview but the screen remained black. There were no messages in the Android monitor so I was baffled. Then it hit me: the camera is lying down on its lens, of course the screen is black. I picked it up, and the camera view showed the correct image. What the heck was I thinking?

Here's the project if you want to follow along: Thermaroid

No comments:

Post a Comment