How save image.jpg from frame?

+1 vote
asked Jun 9, 2018 by dolgopolov (150 points)

I know, how get frame from camera. But i dont know, how frame convert into image in jpg.

I try using byte array, buffer and bitmap, but all failed. Give me nice way for do it, please. 

Frame frame = HelloAR.streamer.peek();
for (Image i : frame.images()) {
    Buffer UnrealB = i.buffer();
    byte[] byteArray = new byte[UnrealB.size()];
    UnrealB.copyTo(byteArray, 0);

    java.nio.ByteBuffer RealBuffer = ByteBuffer.wrap(byteArray);
    RealBuffer.position(0);
    Bitmap bitmap = Bitmap.createBitmap(i.width() , i.height() , Bitmap.Config.RGB_565);
    //error here, stop process -
    bitmap.copyPixelsFromBuffer(RealBuffer);

2 Answers

0 votes
answered Jun 11, 2018 by albert52 (31,850 points)
First of all, I'm not sure what your specific needs are. It doesn't seem to have anything to do with AR. The core function of EasyAR is AR. The EasyAR SDK itself does not have an interface to save the frame as an image. If your requirement is not AR, it's easier to implement this function using opencv.
commented Jun 12, 2018 by dolgopolov (150 points)
I need this so that the user can photograph any object and use it as a target. EasyAR allows take as target only Image.jpg, so i meed save image frome camera and give it TargetManager. You know another way?
0 votes
answered Jul 7, 2018 by derzuomaia (1,190 points)

You did correct, the frame pixels are stored at the byteArray.

The problem is that this pixels array is in the NV21 format. You need to convert to Y or RGB format. You can obtain more info on how to convert here:

https://stackoverflow.com/questions/5272388/extract-black-and-white-image-from-android-cameras-nv21-format/12702836#12702836

If you just wanna the grayscale image you can do that:

private Bitmap getYBitmapFromPixels(byte [] pixels, int width, int height) {
    Bitmap frame_bm = null;
    int [] pixels_int = new int[pixels.length];

    int size = width*height;
    for (int i= 0 ; i<size ; i++) {
        int p = pixels[i] & 0xFF;
        pixels_int[i] = 0xff000000 | p<<16 | p<<8 | p;
    }

    frame_bm = Bitmap.createBitmap(pixels_int, width, height, Bitmap.Config.ARGB_8888);

    return frame_bm;
}

Use:

Buffer UnrealB = frame.images().get(0).buffer();
byte[] byteArray = new byte[UnrealB.size()];
UnrealB.copyTo(byteArray, 0);
Bitmap bitmap = getYBitmapFromPixels(byteArray, frame.images().get(0).width(), frame.images().get(0).height());
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...