7 import android.content.res.AssetManager;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
11 import junit.framework.Assert;
13 import java.io.BufferedInputStream;
15 import java.io.FileInputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
23 public static byte[] readYuvBytesFromFile(String srcFile) {
24 File file =
new File(srcFile);
25 int size = (int) file.length();
26 byte[] bytes =
new byte[size];
28 BufferedInputStream buf =
new BufferedInputStream(
new FileInputStream(file));
29 buf.read(bytes, 0, bytes.length);
31 }
catch (IOException e) {
37 public static void writeYuvBytesToFile(byte[] input,
int width,
int height,
int format, String dstFile) {
38 FileOutputStream fos = null;
40 fos =
new FileOutputStream(
new File(dstFile));
41 fos.write(intToByteArray(width), 0, 4);
42 fos.write(intToByteArray(height), 0, 4);
43 fos.write(intToByteArray(format), 0, 4);
44 fos.write(input, 0, input.length);
46 }
catch (IOException e) {
51 private static void copyFile(InputStream in, OutputStream out)
throws IOException {
52 byte[] buffer =
new byte[1024];
54 while ((read = in.read(buffer)) != -1) {
55 out.write(buffer, 0, read);
59 public static Bitmap getBitmapFromAsset(String fileName, AssetManager assets) {
60 InputStream inputStream = null;
62 inputStream = assets.open(fileName, AssetManager.ACCESS_BUFFER);
64 BufferedInputStream bufferedStream =
new BufferedInputStream(
66 return BitmapFactory.decodeStream(bufferedStream);
67 }
catch (IOException e) {
72 public static byte[] intToByteArray(
int i) {
73 byte[] result =
new byte[4];
75 result[0] = (byte) ((i & 0xFF000000) >> 24);
76 result[1] = (byte) ((i & 0x00FF0000) >> 16);
77 result[2] = (byte) ((i & 0x0000FF00) >> 8);
78 result[3] = (byte) ((i & 0x000000FF));
83 public static int byteArrayToInt(byte[] bytes,
int start) {
84 Assert.assertTrue(
"Byte buffer length is not 4", (bytes.length - start) >= 4);
85 return (bytes[start + 3] & 0xFF) |
86 (bytes[start + 2] & 0xFF) << 8 |
87 (bytes[start + 1] & 0xFF) << 16 |
88 (bytes[start] & 0xFF) << 24;