123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package xu.li.cordova.wechat;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- import android.util.Log;
- import junit.framework.Assert;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- public class Util {
- private static final String TAG = "SDK_Sample.Util";
- public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- bmp.compress(CompressFormat.PNG, 100, output);
- if (needRecycle) {
- bmp.recycle();
- }
-
- byte[] result = output.toByteArray();
- try {
- output.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return result;
- }
-
- public static byte[] getHtmlByteArray(final String url) {
- URL htmlUrl = null;
- InputStream inStream = null;
- try {
- htmlUrl = new URL(url);
- URLConnection connection = htmlUrl.openConnection();
- HttpURLConnection httpConnection = (HttpURLConnection)connection;
- int responseCode = httpConnection.getResponseCode();
- if(responseCode == HttpURLConnection.HTTP_OK){
- inStream = httpConnection.getInputStream();
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- byte[] data = inputStreamToByte(inStream);
- return data;
- }
-
- public static byte[] inputStreamToByte(InputStream is) {
- try{
- ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
- int ch;
- while ((ch = is.read()) != -1) {
- bytestream.write(ch);
- }
- byte imgdata[] = bytestream.toByteArray();
- bytestream.close();
- return imgdata;
- }catch(Exception e){
- e.printStackTrace();
- }
-
- return null;
- }
-
- public static byte[] readFromFile(String fileName, int offset, int len) {
- if (fileName == null) {
- return null;
- }
- File file = new File(fileName);
- if (!file.exists()) {
- Log.i(TAG, "readFromFile: file not found");
- return null;
- }
- if (len == -1) {
- len = (int) file.length();
- }
- Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));
- if(offset <0){
- Log.e(TAG, "readFromFile invalid offset:" + offset);
- return null;
- }
- if(len <=0 ){
- Log.e(TAG, "readFromFile invalid len:" + len);
- return null;
- }
- if(offset + len > (int) file.length()){
- Log.e(TAG, "readFromFile invalid file len:" + file.length());
- return null;
- }
- byte[] b = null;
- try {
- RandomAccessFile in = new RandomAccessFile(fileName, "r");
- b = new byte[len]; // 创建合适文件大小的数组
- in.seek(offset);
- in.readFully(b);
- in.close();
- } catch (Exception e) {
- Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
- e.printStackTrace();
- }
- return b;
- }
-
- private static final int MAX_DECODE_PICTURE_SIZE = 1920 * 1440;
- public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) {
- Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);
- BitmapFactory.Options options = new BitmapFactory.Options();
- try {
- options.inJustDecodeBounds = true;
- Bitmap tmp = BitmapFactory.decodeFile(path, options);
- if (tmp != null) {
- tmp.recycle();
- tmp = null;
- }
- Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
- final double beY = options.outHeight * 1.0 / height;
- final double beX = options.outWidth * 1.0 / width;
- Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
- options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
- if (options.inSampleSize <= 1) {
- options.inSampleSize = 1;
- }
- // NOTE: out of memory error
- while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
- options.inSampleSize++;
- }
- int newHeight = height;
- int newWidth = width;
- if (crop) {
- if (beY > beX) {
- newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
- } else {
- newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
- }
- } else {
- if (beY < beX) {
- newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
- } else {
- newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
- }
- }
- options.inJustDecodeBounds = false;
- Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x" + options.outHeight + ", sample=" + options.inSampleSize);
- Bitmap bm = BitmapFactory.decodeFile(path, options);
- if (bm == null) {
- Log.e(TAG, "bitmap decode failed");
- return null;
- }
- Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
- final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
- if (scale != null) {
- bm.recycle();
- bm = scale;
- }
- if (crop) {
- final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1, (bm.getHeight() - height) >> 1, width, height);
- if (cropped == null) {
- return bm;
- }
- bm.recycle();
- bm = cropped;
- Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
- }
- return bm;
- } catch (final OutOfMemoryError e) {
- Log.e(TAG, "decode bitmap failed: " + e.getMessage());
- options = null;
- }
- return null;
- }
- }
|