1. Are you ready for the Galaxy S20? Here is everything we know so far!

Returning a bitmap from a video properly

Discussion in 'Android Development' started by Michael Knight, Nov 10, 2020.

  1. Michael Knight

    Thread Starter

    I am using the following code to return a bitmap from a video using jni then to display it using c++ and Openframeworks.You may not be familiar with Openframeworks but it's just the last step, possibly there is an error with the code before using Openframeworks' ofImage object.

    These codes are able to grab a frame from a video and then display it but unfortunately instead of one full image it displays four small images next to each other.How can I solve this problem?

    Here is the code:

    Code (Text):
    1. setup() function:
    2. {
    3.  
    4.  
    5. jclass bitmapConfig = (*env)-> FindClass(env,"android/graphics/Bitmap$Config");
    6.  
    7. jfieldID rgba8888FieldID = (*env)->GetStaticFieldID(env,bitmapConfig, "ARGB_8888", "Landroid/graphics/Bitmap$Config;");
    8.  
    9. jobject rgba8888Obj = (*env)->GetStaticObjectField(env,bitmapConfig, rgba8888FieldID);
    10.  
    11. jclass mediadataret = (*env) -> FindClass(env,"android/media/MediaMetadataRetriever");
    12.  
    13. jmethodID medID = (*env)->GetMethodID(env,mediadataret,"<init>","()V");
    14.  
    15. jstring str1 = (*env)->NewStringUTF (env, "/storage/emulated/0/deneme1/MOV_0270.mp4" ) ;
    16.  
    17.  
    18. (*env)->CallVoidMethod(env,mediadatart,medID2,str1);
    19. jclass classbitmap = (*env)->FindClass(env,"android/graphics/Bitmap");
    20.  
    21.  
    22. jmethodID createBitmapMethodID = (*env)()-> GetStaticMethodID(env,classbitmap,"createBitmap",
    23. (IILandroid/graphics/Bitmap$Config;)                                                  
    24. Landroid/graphics/Bitmap;");
    25.  
    26. jobject bitmap1= (*env)->CallStaticObjectMethod(env,classbitmap, createBitmapMethodID, _width,_height, rgba8888Obj);
    27.  
    28.  
    29. jmethodID medID3 = (*env)->GetMethodID(env,mediadataret,"getFrameAtTime","(J)Landroid/graphics/Bitmap;");
    30.  
    31. bitmap1 = (*env)->CallObjectMethod(env,mediadatart,medID3,framenum);
    32.  
    33. jmethodID metID4 = (*env)->GetMethodID(env,classbitmap,"getPixels","([IIIIIII)V");
    34. jmethodID widthget = (*env)->GetMethodID(env,classbitmap,"getWidth","()I");
    35. jmethodID heightget = (*env)->GetMethodID(env,classbitmap,"getHeight","()I");
    36.  
    37.  
    38. jlong framenum=7*1000;
    39.  
    40. jint offset=0;
    41. jint stride=1;
    42. jint x=0;
    43. jint y=0;
    44. jint width= (*env)->CallIntMethod(env,bitmap1,widthget);
    45. jint height = (*env)->CallIntMethod(env,bitmap1,heightget);
    46.  
    47. jintArray pixels = (*env)->NewIntArray(env,width * height*4);
    48.  
    49. (*env)-> CallVoidMethod(env, bitmap1,metID4,pixels,offset,width,x,y,width,height);
    50.  
    51.  
    52. int* pixelsp = new int[width*height*4];
    53.  
    54. (*env)->GetIntArrayRegion(env,pixels,0,width*height,pixelsp);
    55.  
    56. bitmap = new unsigned char[width*height*4];
    57.  
    58.  
    59. for (int y=0; y<height; y++) {
    60.         for (int x=0; x<width; x++) {
    61.          
    62.  
    63.             int index = 4 * ( x + width * y );
    64.             unsigned char red = pixelsp[index]>>16 ;
    65.             unsigned char green = pixelsp[index+1 ]>>8;
    66.             unsigned char blue = pixelsp[index+2];
    67.             unsigned char alpha = pixelsp[index+3] >> 24;
    68.  
    69.  
    70.             bitmap[index]=red;
    71.             bitmap[index+1]=green;
    72.             bitmap[index+2]=blue;
    73.             bitmap[index+3]=alpha;
    74.  
    75.          
    76.  
    77.        }
    78.  
    79. }
    80.  
    81. image1.setFromPixels(bitmap, width, height, ofImageType::OF_IMAGE_COLOR_ALPHA,true);
    82.  
    83. ofBackground(255,255,255);
    84.  
    85. width__=width;
    86. height__=height;
    87.  
    88. }
    89.  
    90.  
    91. draw() function:
    92. {
    93. ofSetColor( 255, 255, 255 );
    94. image1.draw(50,50,width__,height__);
     


    jerrysuntan likes this.

    1. Download the Forums for Android™ app!


      Download

       
  2. Michael Knight

    Thread Starter

    Using:

    Code (Text):
    1.  static int i=0;
    2.  
    3. int index = 4 * ( x + width * y );
    4. unsigned char red = pixelsp[i]>>16 ;
    5. unsigned char green = pixelsp[i]>>8;
    6. unsigned char blue = pixelsp[i];
    7. unsigned char alpha = pixelsp[i] >> 24;
    8.  
    9. pixelsp[index]=red;
    10. pixelsp[index+1]=green;
    11. pixelsp[index+2]=blue;
    12. pixelsp[index+3]=alpha;
    13.  
    14. i++;
    instead of:

    Code (Text):
    1. int index = 4 * ( x + width * y );
    2.             unsigned char red = pixelsp[index]>>16 ;
    3.             unsigned char green = pixelsp[index+1 ]>>8;
    4.             unsigned char blue = pixelsp[index+2];
    5.             unsigned char alpha = pixelsp[index+3] >> 24;
    6.             bitmap[index]=red;
    7.             bitmap[index+1]=green;
    8.             bitmap[index+2]=blue;
    9.             bitmap[index+3]=alpha;

    solved the problem.
     
Loading...

Share This Page

Loading...