Topic locked

Camera Image returns black

August 22, 2014 - 5:38pm #1

Hi. I am trying to get a bitmap from the camera and I am using Vuforia 3.0.9 in Eclipse .

I followed the instructions from this post:

https://developer.vuforia.com/forum/faq/android-how-can-i-access-camera-image 

But this line 

byte[] pixelArray = pixels.array();

Causes the app to crash, so I used another way to convert the ByteBufferer to Byte Array:

byte[] bytes = new byte[buf.limit()];

buf = ByteBuffer.wrap(bytes);

 

// Retrieve bytes between the position and limit

// (see Putting Bytes into a ByteBuffer)

 bytes = new byte[buf.remaining()];

 

// transfer bytes from this buffer into the given destination array

buf.get(bytes, 0, bytes.length);

 

// Retrieve all bytes in the buffer

buf.clear();

bytes = new byte[buf.capacity()];

 

// transfer bytes from this buffer into the given destination array

buf.get(bytes, 0, bytes.length);

 

 

 

Reading the image width and height returns the correct values, but when I display the bitmap it comes out all black. Is there another way to acces the images com the camera?

 

Thanks!

Camera Image returns black

September 9, 2014 - 6:38am #18

Rotating the image is no longer a Vuforia topic. You can probably search this in other Forums (e.g. Android and general  image processing forums).

 

 

Camera Image returns black

September 9, 2014 - 6:02am #17

Can I rotate the Image which I get from the vuforia sdk? I want the image to be rotated in 90 degree clockwise. I tried

Matrix matrix = new Matrix();
// rotate the Bitmap
matrix.postRotate(90);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,width, height, matrix, true);

It Works fine but it reduces the performance of the app. Is there any faster(impoved performance) way of rotating.

Camera Image returns black

September 7, 2014 - 11:42pm #16

I guess most common image processing tasks would need access to a byte array (pixel values), but Android also supports some processing directly on a Bitmap (for example scaling / resizing a bitmap);    but this is not something that relates directly to Vuforia (as there is no image processing API in the SDK),

and it depends pretty much on what type of image processing you want to do and what libraries or functions you use.

 

Camera Image returns black

September 7, 2014 - 11:28pm #15

I want to know is it good to use the bitmap than the byte[] for processing camera data (performance wise) on android device.

since the quality of the yuv is poor I thought to use the bitmap insted of byte[].

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(buffer));  

 

Camera Image returns black

September 7, 2014 - 10:12am #14

So, the RGB565 looks fine

but the YUV quality seems to be poor (I can see from the screenshots too);

I would suggest to check that you correctly encode the YUV image when saving it to file;

perhaps you may want to use the YuvImage class of Android SDK:

http://developer.android.com/reference/android/graphics/YuvImage.html

If you are sure that the YUV image is correctly encoded when saving to PNG, then it might be that the source camera image (as delivered from the device camera) is simply not great quality.  Vuforia does not have specific API to improve this, as it simply grabs the camera image from the underlying hardware.

 

Camera Image returns black

September 6, 2014 - 6:37am #13

I'm trying to get YUV format data and I get the image but the quality of image using YUV is poor. Is there any way to improve the Quality.

AttachmentSize
Image icon YUV.png358.9 KB
Image icon RGB565.png580.86 KB

Camera Image returns black

September 4, 2014 - 6:50am #12

Are you executing the code  from the onQCARUpdate() method ? You need to get the pixels in that callback to get the camera image safely. You can try using the ImageTargets sample (see code in ImageTargets.java).

Using the code below I get a good camera image.

Also, make sure you have set the Frame Format to be RGB565.

 

 

Camera Image returns black

September 4, 2014 - 6:28am #11

This is the image I get using your code. i have attached saved image.

AttachmentSize
Image icon test_10.png2.71 KB

Camera Image returns black

September 4, 2014 - 5:24am #10

Make sure to use the actual image width and height (and NOT the hardcoded values 700,1100).

if (imageRGB565 != null) {
  int imageWidth = imageRGB565.getWidth();
  int imageHeight = imageRGB565.getHeight();
  int stride = imageRGB565.getStride();
  ByteBuffer pixels = imageRGB565.getPixels();
  byte[] pixelArray = new byte[pixels.remaining()];
  pixels.get(pixelArray, 0, pixelArray.length);
                  
  saveCapturedImage(pixelArray, imageWidth, imageHeight);
}

Also, not sure about how you save the image (as you did not post your implementation code for the "saveImage" function);

this is the one I use:

public void saveCapturedImage(byte[] buffer, int width, int height) 
{
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(buffer));  
  try {
    OutputStream stream; = new FileOutputStream("/sdcard/test.png");
    bitmap.compress(CompressFormat.PNG, 80, stream);
    stream.close();
  } catch (IOException e) {
  }
}

And this works fine for me.

 

ALSO: make sure you call the function above from the onQCARUpdate() (see ImageTargets.java in the samples).

 

Camera Image returns black

September 4, 2014 - 4:40am #9

I have tried it and when i convert that array to bitmap it says null. Here is the code.

byte[] pixelArray = new byte[pixels.remaining()];

pixels.get(pixelArray, 0, pixelArray.length);

Bitmap camImage = BitmapFactory.decodeByteArray(pixelArray, 0, pixelArray.length);

saveImage(camImage);

 

And this is the log message.

--- SkImageDecoder::Factory returned null.

 : java.lang.NullPointerException.

 

 

 

 

Camera Image returns black

September 4, 2014 - 4:38am #8

I have tried this code 

 

if (image.getFormat() == PIXEL_FORMAT.RGB565 ) 
{             
     imageRGB565 = image; 
     ByteBuffer pixels = imageRGB565.getPixels();
     if(pixels.get()!=0)
     {
           Bitmap screenCapture = Bitmap.createBitmap(700,1100,Config.RGB_565);
           screenCapture.copyPixelsFromBuffer(pixels);
           saveImage(screenCapture);
     }
}

 

The format of the created image and the byteBuffer is same.

I have attached the saved image.

AttachmentSize
Image icon 1snap.png302.73 KB

Camera Image returns black

September 3, 2014 - 7:01am #7

Concerning the error / crash in the line "byte[] pixelArray = pixels.array();"

you can replace it with:

byte[] pixelArray = new byte[pixels.remaining()];
pixels.get(pixelArray, 0, pixelArray.length);

This should work without any error or crash (I have also updated the code snippet in the FAQ article

https://developer.vuforia.com/forum/faq/android-how-can-i-access-camera-image).

 

Concerning the performance: reading the camera frame pixels has some performance cost, obviously; so, it is OK for taking snapshots (e.g. to save a picture or so), but it is not ideal for continuous capturing (e.g. not good if you want to capture every single frame).

The other approach suggested by HOIXAIA is typicaly faster (as it relies on OpenGL), but still it may hit performance a little bit (e.g. resulting in slightly lower frame rate) as the approach is based on the OpenGL function call "glReadPixels()", which is known to be an expensive / slow OpenGL call (especially if the buffer is large).

I am not aware of other (faster) approaches to get better results, but you may want to also search some OpenGL specific forums for possible alternative to glReadPixels().

 

 

Camera Image returns black

September 1, 2014 - 12:51am #6

Thanks for the link it works but it hits performance. I need to continiously get the pixels for processing.

Is it possible to improve the performance in openGL? 

Camera Image returns black

August 27, 2014 - 3:20pm #5

Yes I had the same error.

In the end I used the code in this link:

https://developer.vuforia.com/forum/faq/android-how-can-i-capture-ar-view

Hope it helps! :)

Camera Image returns black

August 25, 2014 - 10:10pm #4

This is the error I get on this line byte[] pixelsArray = pixels.array();

java.lang.UnsupportedOperationException.

Camera Image returns black

August 25, 2014 - 2:35pm #3

No, not yet. I think the Vuforia Docs are a bit outdated.

Camera Image returns black

August 25, 2014 - 4:56am #2

Hey did you find the solution coz I've got the same error and when i use  byte[] pixelArray = pixels.array();

It gives me errors and app crashes, and while converting those pixels to bitmap using bitmapfactory it returns null Bitmap.

 

Thanks,

Topic locked