MaxstARSDK  3.5.0
All Classes Functions Variables
MaxstARUtil.java
1 /*
2  * Copyright 2016 Maxst, Inc. All Rights Reserved.
3  */
4 
5 package com.maxst.ar;
6 
7 import android.content.res.AssetManager;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
10 
11 import junit.framework.Assert;
12 
13 import java.io.BufferedInputStream;
14 import java.io.File;
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;
20 
21 public class MaxstARUtil {
22 
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];
27  try {
28  BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
29  buf.read(bytes, 0, bytes.length);
30  buf.close();
31  } catch (IOException e) {
32  e.printStackTrace();
33  }
34  return bytes;
35  }
36 
37  public static void writeYuvBytesToFile(byte[] input, int width, int height, int format, String dstFile) {
38  FileOutputStream fos = null;
39  try {
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);
45  fos.close();
46  } catch (IOException e) {
47  e.printStackTrace();
48  }
49  }
50 
51  private static void copyFile(InputStream in, OutputStream out) throws IOException {
52  byte[] buffer = new byte[1024];
53  int read;
54  while ((read = in.read(buffer)) != -1) {
55  out.write(buffer, 0, read);
56  }
57  }
58 
59  public static Bitmap getBitmapFromAsset(String fileName, AssetManager assets) {
60  InputStream inputStream = null;
61  try {
62  inputStream = assets.open(fileName, AssetManager.ACCESS_BUFFER);
63 
64  BufferedInputStream bufferedStream = new BufferedInputStream(
65  inputStream);
66  return BitmapFactory.decodeStream(bufferedStream);
67  } catch (IOException e) {
68  return null;
69  }
70  }
71 
72  public static byte[] intToByteArray(int i) {
73  byte[] result = new byte[4];
74 
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));
79 
80  return result;
81  }
82 
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;
89  }
90 }