11package io .agora .api .example .utils ;
22
3+ import static android .renderscript .Element .RGBA_8888 ;
4+ import static android .renderscript .Element .U8 ;
5+ import static android .renderscript .Element .U8_4 ;
6+
37import android .content .Context ;
48import android .graphics .Bitmap ;
59import android .graphics .BitmapFactory ;
812import android .graphics .Rect ;
913import android .graphics .YuvImage ;
1014import android .renderscript .Allocation ;
11- import android .renderscript .Element ;
15+ import android .renderscript .Type . Builder ;
1216import android .renderscript .RenderScript ;
1317import android .renderscript .ScriptIntrinsicBlur ;
18+ import android .renderscript .ScriptIntrinsicYuvToRGB ;
1419
1520import java .io .ByteArrayOutputStream ;
1621import java .io .IOException ;
22+ import java .nio .ByteBuffer ;
1723
1824public class YUVUtils {
1925
@@ -124,7 +130,7 @@ public static Bitmap blur(Context context, Bitmap image, float radius) {
124130 Bitmap outputBitmap = Bitmap .createBitmap (image .getWidth (), image .getHeight (), Bitmap .Config .ARGB_8888 );
125131 Allocation in = Allocation .createFromBitmap (rs , image );
126132 Allocation out = Allocation .createFromBitmap (rs , outputBitmap );
127- ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur .create (rs , Element . U8_4 (rs ));
133+ ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur .create (rs , U8_4 (rs ));
128134 intrinsicBlur .setRadius (radius );
129135 intrinsicBlur .setInput (in );
130136 intrinsicBlur .forEach (out );
@@ -145,4 +151,70 @@ public static byte[] bitmapToI420(int inputWidth, int inputHeight, Bitmap scaled
145151 return yuv ;
146152 }
147153
154+ public static byte [] toWrappedI420 (ByteBuffer bufferY ,
155+ ByteBuffer bufferU ,
156+ ByteBuffer bufferV ,
157+ int width ,
158+ int height ) {
159+ int chromaWidth = (width + 1 ) / 2 ;
160+ int chromaHeight = (height + 1 ) / 2 ;
161+ int lengthY = width * height ;
162+ int lengthU = chromaWidth * chromaHeight ;
163+ int lengthV = lengthU ;
164+
165+
166+ int size = lengthY + lengthU + lengthV ;
167+
168+ byte [] out = new byte [size ];
169+ for (int i = 0 ; i < size ; i ++) {
170+ if (i < lengthY ) {
171+ out [i ] = bufferY .get (i );
172+ } else if (i < lengthY + lengthU ) {
173+ int j = (i - lengthY ) / chromaWidth ;
174+ int k = (i - lengthY ) % chromaWidth ;
175+ out [i ] = bufferU .get (j * width + k );
176+ } else {
177+ int j = (i - lengthY - lengthU ) / chromaWidth ;
178+ int k = (i - lengthY - lengthU ) % chromaWidth ;
179+ out [i ] = bufferV .get (j * width + k );
180+ }
181+ }
182+
183+ return out ;
184+ }
185+ /**
186+ * I420转nv21
187+ */
188+ public static byte [] I420ToNV21 (byte [] data , int width , int height ) {
189+ byte [] ret = new byte [data .length ];
190+ int total = width * height ;
191+
192+ ByteBuffer bufferY = ByteBuffer .wrap (ret , 0 , total );
193+ ByteBuffer bufferVU = ByteBuffer .wrap (ret , total , total / 2 );
194+
195+ bufferY .put (data , 0 , total );
196+ for (int i = 0 ; i < total / 4 ; i += 1 ) {
197+ bufferVU .put (data [i + total + total / 4 ]);
198+ bufferVU .put (data [total + i ]);
199+ }
200+
201+ return ret ;
202+ }
203+
204+ public static Bitmap NV21ToBitmap (Context context , byte [] nv21 , int width , int height ) {
205+ RenderScript rs = RenderScript .create (context );
206+ ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB .create (rs , U8_4 (rs ));
207+ Builder yuvType = null ;
208+ yuvType = (new Builder (rs , U8 (rs ))).setX (nv21 .length );
209+ Allocation in = Allocation .createTyped (rs , yuvType .create (), 1 );
210+ Builder rgbaType = (new Builder (rs , RGBA_8888 (rs ))).setX (width ).setY (height );
211+ Allocation out = Allocation .createTyped (rs , rgbaType .create (), 1 );
212+ in .copyFrom (nv21 );
213+ yuvToRgbIntrinsic .setInput (in );
214+ yuvToRgbIntrinsic .forEach (out );
215+ Bitmap bmpout = Bitmap .createBitmap (width , height , Bitmap .Config .ARGB_8888 );
216+ out .copyTo (bmpout );
217+ return bmpout ;
218+ }
219+
148220}
0 commit comments