nowindxdw
11/2/2017 - 2:05 AM

src 字幕测试代码

    //test src view
    //todo need remove after src is done
    private ImageView srtView;
    private void showSrcText() {
        //new srt view to show
        srtView = new ImageView(PlayerSingleton.getInstance().getContext());
        //通过资源文件获取BMP
//        BitmapFactory.Options op = new BitmapFactory.Options();
//        op.inPreferredConfig = Bitmap.Config.ARGB_8888;
//        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.sub,op);
        //通过路径获取BMP
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//        Bitmap bmp = BitmapFactory.decodeFile("/lesee/res/raw/sub",options);
        //通过raw获取BMP
        InputStream is = getResources().openRawResource(R.raw.sub);
        byte[] imgByte = new byte[0];
        try {
            imgByte = readBytes(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Logger.d("imglength="+imgByte.length);//103734
        Bitmap bmp=createBitmapFromPointsArray(0,448,720,54,imgByte,imgByte.length);
        //通过二进制流获取BMP
        //Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null,  bmpFactoryOptions);
        Logger.d("imgBMP="+bmp);//103734
        srtView.setImageBitmap(bmp);
        //设置字幕图层的宽度与高度
        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置字幕图层的位置
        param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        srtView.setPadding(0, 0, 0, 70);
        //添加图层
        splash_rl = (RelativeLayout)findViewById(R.id.splash_rl);
        splash_rl.addView(srtView, param);
    }

//    int x          display star position int px
//    int y           display star position inp px
//    int width        bitmap width px,  one line pixel bytes=width*4
//    int offset       bitmap pixel data=data+offset   bytes
//    byte[] data   bitmap file data  (pixel fmt:ARGB)
//    int length       bitmap file length, then  pixel data length=length-offset  bytes
    private static Bitmap createBitmapFromPointsArray (int x,int y,int bmp_width,int offset,byte[] imgbyte,int length){

        // try draw canvas below
        //width,height        字幕区域ImageView宽度和高度
        int width = 720;
        int height = 36;
        //字幕点阵宽度和高度
        int w =bmp_width;//720
        int h = (length-offset)/(bmp_width*4);//36
        // 若读取图片的宽度或高度小于ImageView的宽度或高度,则对图片进行放大
        //        if (w < width || h < height) {
        //            Matrix matrix = new Matrix();
        //            matrix.postScale((float) width / w, (float) height / h); // 长和宽放大缩小的比例
        //            Bitmap bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        //        }
        // 创建一个新的bitmap,然后在bitmap里创建一个画布,将之前的图片画在里面。

        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        //每一个点的数组
        byte[] buffer = imgbyte;
        //绘制起点坐标x,y
        //每一行的宽度
        int lineWidth = bmp_width;//lineWidth = 720
        int pointCount;
        int totalPoint = (length-offset)/4; //total point=25920
        byte[] pointA = new byte[totalPoint];
        byte[] pointR = new byte[totalPoint];
        byte[] pointG = new byte[totalPoint];
        byte[] pointB = new byte[totalPoint];
        for(int index=offset;index<length;index++){
            pointCount = (index-offset)/4;
            if(index%4 ==0){
                pointA[pointCount] = buffer[index];
//                pointA[pointCount] = (byte)0x00;
            }
            if(index%4 ==1){
                pointR[pointCount] = buffer[index];
//                pointR[pointCount] = (byte)0x00;
            }
            if(index%4 ==2){
                pointG[pointCount] = buffer[index];
//                pointG[pointCount] = (byte)0x00;
            }
            if(index%4 ==3){
                pointB[pointCount] = buffer[index];
//                pointB[pointCount] = (byte)0xff;
            }
        }
        for (int p=0;p<totalPoint;p++){
            final Paint paint = new Paint();
            canvas.drawARGB(pointA[p], pointR[p], pointG[p], pointB[p]);
            int X = p%lineWidth;
            int Y = p/lineWidth;
            canvas.drawPoint(x+X,y+Y,paint);
        }
        return output;
    }

    private void bmpRecycle(Bitmap bmp){
        if(bmp != null && !bmp.isRecycled()){
            bmp.recycle();
            System.gc();
            bmp = null;
        }
    }

    private void hideSrcText() {
        if (srtView != null)
            splash_rl = (RelativeLayout)findViewById(R.id.splash_rl);
            splash_rl.removeView(srtView);
    }

    public static byte[] readBytes(InputStream in) throws IOException {
        byte[] temp = new byte[in.available()];
        byte[] result = new byte[0];
        int size = 0;
        while ((size = in.read(temp)) != -1) {
            byte[] readBytes = new byte[size];
            System.arraycopy(temp, 0, readBytes, 0, size);
            result = mergeArray(result,readBytes);
        }
        return result;
    }
    public static byte[] mergeArray(byte[]... a) {
        // 合并完之后数组的总长度
        int index = 0;
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum = sum + a[i].length;
        }
        byte[] result = new byte[sum];
        for (int i = 0; i < a.length; i++) {
            int lengthOne = a[i].length;
            if(lengthOne==0){
                continue;
            }
            // 拷贝数组
            System.arraycopy(a[i], 0, result, index, lengthOne);
            index = index + lengthOne;
        }
        return result;
    }
}