1. Download our Official Android App: Forums for Android!

Apps Different code(.java file) for different platform?

Discussion in 'Android Development' started by Rudba556, Mar 2, 2016.

  1. Rudba556

    Rudba556 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Oct 27, 2015

    Oct 27, 2015
    3
    0
    5
    Male
    freelancer
    I have a code where image data is passed from bitmap to FFmpeg frame recorder and converted to a video. But i need to make small changes while running it on LG G3(armv7) from Asus zenfone 5(x86).

    Following are the class variables that create the issue:(declared under, class Main Activity)

    inputWidth = 1024;
    inputHeight = 650;

    Following is the method where the issue occurs:
    Code (Text):
    1.  byte [] getNV21(int inputWidth, int inputHeight, Bitmap bitmap) {
    2.  
    3.         int [] argb = new int[inputWidth * inputHeight];
    4.  
    5.         bitmap.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
    6.  
    7.         byte [] yuv = new byte[inputWidth*inputHeight*3/2];
    8.         encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
    9.  
    10.         return yuv;
    11.     }
    12.  
    13.     void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
    14.         final int frameSize = width * height;
    15.  
    16.         int yIndex = 0;
    17.         int uvIndex = frameSize;
    18.  
    19.         int a, R, G, B, Y, U, V;
    20.         int index = 0;
    21.         for (int j = 0; j < height; j++) {
    22.             for (int i = 0; i < width; i++) {
    23.  
    24.                 a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
    25.                 R = (argb[index] & 0xff0000) >> 16;
    26.                 G = (argb[index] & 0xff00) >> 8;
    27.                 B = (argb[index] & 0xff) >> 0;
    28.  
    29.                 // well known RGB to YUV algorithm
    30.                 Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16;
    31.                 U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128;
    32.                 V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128;
    33.  
    34.                 // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
    35.                 //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
    36.                 //    pixel AND every other scanline.
    37.                 yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
    38.                 if (j % 2 == 0 && index % 2 == 0) {
    39.                     yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
    40.                     yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
    41.                 }
    42.  
    43.                 index ++;
    44.             }
    45.         }
    46.     }
    Working CODE:
    LG G3 :I can use the above variables at any place in the code to get the required output.
    Bitmap size returned = 2734200

    Asus Zenfone 5: Except at creating the bitmap, I have to use everywhere else bitmap.getHeight() and bitmap.getWidth(), to get the required output.
    Surprisingly here Bitmap size returned = 725760

    INCORRECT CODE:
    LG G3 : IF i use bitmap.getHeight() and bitmap.getWidth(), i get java.lang.ArrayIndexOutOfBoundsException: length = 102354 , index = 102354. @getNV21 method

    Asus Zenfone 5 : If i use inputWidth , inputHeight i get
    java.lang.IllegalArgumentException: x + width must be <= bitmap.width() @getNV21 method

    How can i generalize the above code for both phones?
     

    Advertisement

Share This Page

Loading...