Difference between revisions of "Android support"

From BoofCV
Jump to navigationJump to search
Line 31: Line 31:


Full Examples:
Full Examples:
* [[Example_Android_Video|Android Video]]
* [[Example_Android_Gradient|Visualize Gradient]]
* [[https://github.com/lessthanoptimal/BoofCV/blob/SNAPSHOT/integration/boofcv-android/examples/video/app/src/main/java/org/boofcv/video/ExposureActivity.java Exposure Control]]
* [https://github.com/lessthanoptimal/BoofCV/blob/SNAPSHOT/integration/boofcv-android/examples/video/app/src/main/java/org/boofcv/video/ExposureActivity.java Exposure Control]
* [[https://github.com/lessthanoptimal/BoofCV/blob/SNAPSHOT/integration/boofcv-android/examples/video/app/src/main/java/org/boofcv/video/QrCodeActivity.java QR Code Detect and Visualize]]
* [https://github.com/lessthanoptimal/BoofCV/blob/SNAPSHOT/integration/boofcv-android/examples/video/app/src/main/java/org/boofcv/video/QrCodeActivity.java QR Code Detect and Visualize]


== Converting Bitmap images ==
== Converting Bitmap images ==
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
    // Easiest way to convert a Bitmap into a BoofCV type
// Easiest way to convert a Bitmap into a BoofCV type
    GrayU8 image = ConvertBitmap.bitmapToGray(bitmap, (GrayU8)null, null);
GrayU8 image = ConvertBitmap.bitmapToGray(bitmap, (GrayU8)null, null);
    // If you are converting a sequence of images it is faster reuse a
// If you are converting a sequence of images it is faster reuse a
    // previously declare image and buffer
// previously declare image and buffer
    byte[] workBuffer = ConvertBitmap.declareStorage(bitmap, null);
byte[] workBuffer = ConvertBitmap.declareStorage(bitmap, null);
    ConvertBitmap.bitmapToGray(bitmap, image, workBuffer);
ConvertBitmap.bitmapToGray(bitmap, image, workBuffer);
    
    
    // Convert back into a Bitmap
// Convert back into a Bitmap
    ConvertBitmap.grayToBitmap(image, bitmap, workBuffer);
ConvertBitmap.grayToBitmap(image, bitmap, workBuffer);


    // another less efficient way
// another less efficient way
    bitmap = ConvertBitmap.grayToBitmap(image, Bitmap.Config.ARGB_8888);
bitmap = ConvertBitmap.grayToBitmap(image, Bitmap.Config.ARGB_8888);
    
    
    // Functions are also provided for multi-spectral images
// Functions are also provided for multi-spectral images
    Planar<GrayF32> color = ConvertBitmap.bitmapToMS(bitmap, null, GrayF32.class, null);
Planar<GrayF32> color = ConvertBitmap.bitmapToMS(bitmap, null, GrayF32.class, null);
</syntaxhighlight>
 
== Converting YUV420 Images ==
 
<syntaxhighlight lang="java">
Image image; // From camera in YUV 420 format
byte work[] = ConvertCameraImage.declareWork(image,null);
ConvertCameraImage.imageToBoof(image, ColorFormat.RGB, grayU8, work);
</syntaxhighlight>
</syntaxhighlight>


== Converting NV21 Images ==
== Converting NV21 Images ==
Data from camera previews is made available in the NV21 image format.
Data from camera previews in Camera 1 API is made available in the NV21 image format.


<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
    // from NV21 to gray scale
// from NV21 to gray scale
    ConvertNV21.nv21ToGray(bytes,width,height,gray)
ConvertNV21.nv21ToGray(bytes,width,height,gray)


  // from NV21 to YUV Multi-Spectral
// from NV21 to YUV Multi-Spectral
    ConvertNV21.nv21ToPlanarYuv(bytes,width,height,colorMS,GrayU8.class);
ConvertNV21.nv21ToPlanarYuv(bytes,width,height,colorMS,GrayU8.class);
</syntaxhighlight>
</syntaxhighlight>



Revision as of 21:39, 12 November 2018

BoofCV works without modification on Android systems, however you will need to convert Android specific image types into BoofCV image types. Support is provided for converting Bitmap, NV21, and YUV420 images. Camera 2 API is actively supported while camera 1 API is unsupported but can still be used.

To use BoofCV in your project all you need to do is add the following line to dependencies in app/build.gradle

api group: 'org.boofcv', name: 'boofcv-android', version: '0.31'

No need to add jars manually, cut-and-paste BoofCV's source, sacrifice a chicken, or whatever weird stuff you guys keep on wanting to do. It's very simple. Well that's a little bit of a lie. You might also want to add the following to your app/build.gradle file since in some versions there's a conflict:

// conflicts with a Android stuff
configurations {
    all*.exclude group: "xmlpull", module: "xmlpull"
    all*.exclude group: "org.apache.commons", module: "commons-compress"
    all*.exclude group: "com.thoughtworks.xstream", module: "commons-compress"
}

Trouble Shooting

Does your code run much slower than the demo? Try switching the build to release. Click on "Build Variants" on the bottom left, then in the panel that pops up select "release" from the build variant.

Usage Examples

A more detailed and working example for processing video images in Android can be found at the link below. Short code snippets are included in this section.

Full Examples:

Converting Bitmap images

// Easiest way to convert a Bitmap into a BoofCV type
GrayU8 image = ConvertBitmap.bitmapToGray(bitmap, (GrayU8)null, null);
	
// If you are converting a sequence of images it is faster reuse a
// previously declare image and buffer
byte[] workBuffer = ConvertBitmap.declareStorage(bitmap, null);
ConvertBitmap.bitmapToGray(bitmap, image, workBuffer);
    	
// Convert back into a Bitmap
ConvertBitmap.grayToBitmap(image, bitmap, workBuffer);

// another less efficient way
bitmap = ConvertBitmap.grayToBitmap(image, Bitmap.Config.ARGB_8888);
    	
// Functions are also provided for multi-spectral images
Planar<GrayF32> color = ConvertBitmap.bitmapToMS(bitmap, null, GrayF32.class, null);

Converting YUV420 Images

Image image; // From camera in YUV 420 format
byte work[] = ConvertCameraImage.declareWork(image,null);
ConvertCameraImage.imageToBoof(image, ColorFormat.RGB, grayU8, work);

Converting NV21 Images

Data from camera previews in Camera 1 API is made available in the NV21 image format.

// from NV21 to gray scale
ConvertNV21.nv21ToGray(bytes,width,height,gray)

// from NV21 to YUV Multi-Spectral
ConvertNV21.nv21ToPlanarYuv(bytes,width,height,colorMS,GrayU8.class);

Besides converting images, VisualizeImageData is provided for visualizing

Android Apps

Android demonstration and benchmark applications are available for download from the google play store.

The demonstration visualizes several computer vision algorithms and provides a good estimate on what sort of runtime performance can be expected when using BoofCV on Android.