Util.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package xu.li.cordova.wechat;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Bitmap.CompressFormat;
  4. import android.graphics.BitmapFactory;
  5. import android.util.Log;
  6. import junit.framework.Assert;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.RandomAccessFile;
  12. import java.net.HttpURLConnection;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16. public class Util {
  17. private static final String TAG = "SDK_Sample.Util";
  18. public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
  19. ByteArrayOutputStream output = new ByteArrayOutputStream();
  20. bmp.compress(CompressFormat.PNG, 100, output);
  21. if (needRecycle) {
  22. bmp.recycle();
  23. }
  24. byte[] result = output.toByteArray();
  25. try {
  26. output.close();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. return result;
  31. }
  32. public static byte[] getHtmlByteArray(final String url) {
  33. URL htmlUrl = null;
  34. InputStream inStream = null;
  35. try {
  36. htmlUrl = new URL(url);
  37. URLConnection connection = htmlUrl.openConnection();
  38. HttpURLConnection httpConnection = (HttpURLConnection)connection;
  39. int responseCode = httpConnection.getResponseCode();
  40. if(responseCode == HttpURLConnection.HTTP_OK){
  41. inStream = httpConnection.getInputStream();
  42. }
  43. } catch (MalformedURLException e) {
  44. e.printStackTrace();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. byte[] data = inputStreamToByte(inStream);
  49. return data;
  50. }
  51. public static byte[] inputStreamToByte(InputStream is) {
  52. try{
  53. ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
  54. int ch;
  55. while ((ch = is.read()) != -1) {
  56. bytestream.write(ch);
  57. }
  58. byte imgdata[] = bytestream.toByteArray();
  59. bytestream.close();
  60. return imgdata;
  61. }catch(Exception e){
  62. e.printStackTrace();
  63. }
  64. return null;
  65. }
  66. public static byte[] readFromFile(String fileName, int offset, int len) {
  67. if (fileName == null) {
  68. return null;
  69. }
  70. File file = new File(fileName);
  71. if (!file.exists()) {
  72. Log.i(TAG, "readFromFile: file not found");
  73. return null;
  74. }
  75. if (len == -1) {
  76. len = (int) file.length();
  77. }
  78. Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));
  79. if(offset <0){
  80. Log.e(TAG, "readFromFile invalid offset:" + offset);
  81. return null;
  82. }
  83. if(len <=0 ){
  84. Log.e(TAG, "readFromFile invalid len:" + len);
  85. return null;
  86. }
  87. if(offset + len > (int) file.length()){
  88. Log.e(TAG, "readFromFile invalid file len:" + file.length());
  89. return null;
  90. }
  91. byte[] b = null;
  92. try {
  93. RandomAccessFile in = new RandomAccessFile(fileName, "r");
  94. b = new byte[len]; // 创建合适文件大小的数组
  95. in.seek(offset);
  96. in.readFully(b);
  97. in.close();
  98. } catch (Exception e) {
  99. Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
  100. e.printStackTrace();
  101. }
  102. return b;
  103. }
  104. private static final int MAX_DECODE_PICTURE_SIZE = 1920 * 1440;
  105. public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) {
  106. Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);
  107. BitmapFactory.Options options = new BitmapFactory.Options();
  108. try {
  109. options.inJustDecodeBounds = true;
  110. Bitmap tmp = BitmapFactory.decodeFile(path, options);
  111. if (tmp != null) {
  112. tmp.recycle();
  113. tmp = null;
  114. }
  115. Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
  116. final double beY = options.outHeight * 1.0 / height;
  117. final double beX = options.outWidth * 1.0 / width;
  118. Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
  119. options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
  120. if (options.inSampleSize <= 1) {
  121. options.inSampleSize = 1;
  122. }
  123. // NOTE: out of memory error
  124. while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
  125. options.inSampleSize++;
  126. }
  127. int newHeight = height;
  128. int newWidth = width;
  129. if (crop) {
  130. if (beY > beX) {
  131. newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
  132. } else {
  133. newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
  134. }
  135. } else {
  136. if (beY < beX) {
  137. newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
  138. } else {
  139. newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
  140. }
  141. }
  142. options.inJustDecodeBounds = false;
  143. Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x" + options.outHeight + ", sample=" + options.inSampleSize);
  144. Bitmap bm = BitmapFactory.decodeFile(path, options);
  145. if (bm == null) {
  146. Log.e(TAG, "bitmap decode failed");
  147. return null;
  148. }
  149. Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
  150. final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
  151. if (scale != null) {
  152. bm.recycle();
  153. bm = scale;
  154. }
  155. if (crop) {
  156. final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1, (bm.getHeight() - height) >> 1, width, height);
  157. if (cropped == null) {
  158. return bm;
  159. }
  160. bm.recycle();
  161. bm = cropped;
  162. Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
  163. }
  164. return bm;
  165. } catch (final OutOfMemoryError e) {
  166. Log.e(TAG, "decode bitmap failed: " + e.getMessage());
  167. options = null;
  168. }
  169. return null;
  170. }
  171. }