androiddemosaicing

How to demosaic a bayer RAW image?


I have a proprietary bayer RAW image which I need to demosaic on Android to display the image. I am reading the file header byte by byte to retrieve the resolution info (imageWidth/imageHeight). After the header there is the pixel data from byte 2000 and following. How can I demosaic this bayer pixel data on Android to display the image?

Here is my code which reads the byte data and retreives the resolution:

File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
    buf.read(bytes, 0, bytes.length);
    buf.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

int x1 = bytes[1056];
int x2 = bytes[1057];
int x3 = bytes[1058];
int x4 = bytes[1059];
int imageWidth = (x1 << 24) + (x2 << 16) + (x3 << 8) + x4;

int y1 = bytes[1056];
int y2 = bytes[1057];
int y3 = bytes[1058];
int y4 = bytes[1059];
int imageHeigth = (y1 << 24) + (y2 << 16) + (y3 << 8) + y4;

// image data bytes[2000] and following, one pixel per byte
//
// bayer format:
//
// BGBGBGBG....
// GRGRGRGR....
// BGBGBGBG....
// ............
// ............

Solution

  • You can demosaic a bayer RAW image using a third party libaray OpenCV. It has a function Imgproc.demosaicing() that converts a raw Bayer image to desired format

    You can lookup this cpp of OpenCV that can be used in android.

    https://github.com/opencv/opencv/blob/master/modules/imgproc/src/demosaicing.cpp

    Hope this will help you.