Пример #1
0
  private Bitmap extentSnapshot(int doc, int gs, int spaceAround, boolean transparent) {
    final LogHelper log = new LogHelper();
    final Rect extent = getDisplayExtent();

    if (!extent.isEmpty()) {
      extent.inset(-spaceAround, -spaceAround);
      extent.intersect(0, 0, mView.getView().getWidth(), mView.getView().getHeight());
    }
    if (extent.isEmpty()) {
      return null;
    }

    final Bitmap viewBitmap = mView.snapshot(doc, gs, transparent);
    if (viewBitmap == null) {
      return null;
    }

    if (extent.width() == mView.getView().getWidth()
        && extent.height() == mView.getView().getHeight()) {
      log.r(viewBitmap.getByteCount());
      return viewBitmap;
    }

    final Bitmap realBitmap =
        Bitmap.createBitmap(viewBitmap, extent.left, extent.top, extent.width(), extent.height());

    viewBitmap.recycle();
    log.r(realBitmap.getByteCount());
    return realBitmap;
  }
Пример #2
0
  private boolean check() {
    if (mMenuTitleValue.getText().toString().equals("")
        || mMenuContentValue.getText().toString().equals("")
        || mCookingTimeValue.getText().toString().equals("")) {
      Toast.makeText(AddMenuActivity.this, "Please fill required text fields!", Toast.LENGTH_SHORT)
          .show();
      return false;
    } else if (mDisplayImgView.getDrawable() == null) {
      Toast.makeText(AddMenuActivity.this, "Please upload a display image!", Toast.LENGTH_SHORT)
          .show();
      return false;
    } else {
      //            BitmapFactory.Options options = new BitmapFactory.Options();
      //            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
      mDisPlayBitmap = BitmapFactory.decodeFile(mDisplayImgPath.get(0));

      // Compress img to meet the ParseFile maximum requirements
      if (mDisPlayBitmap.getByteCount() >= 10485760)
        mDisPlayBitmap = Bitmap.createScaledBitmap(mDisPlayBitmap, 1920, 1080, true);
      for (int i = 0; i < mToeRecyclerAdapter.getItemCount(); i++) {
        Bitmap currentImg = BitmapFactory.decodeFile(mImgPaths.get(i));
        if (currentImg.getByteCount() >= 10485760)
          currentImg = Bitmap.createScaledBitmap(currentImg, 1920, 1080, true);
        mImgBitmaps.add(currentImg);
      }
      return true;
    }
  }
  /**
   * This is a test for BitmapConverter. From the test below we can see that BitmapConverter can
   * both serizlize and deserizlize
   */
  public void testLBitmapConverter() throws Exception {

    JsonElement jsonElement = bitmapConverter.serialize(bitmap, null, null);
    Bitmap newBitmap = bitmapConverter.deserialize(jsonElement, null, null);

    assertEquals(bitmap.getByteCount(), newBitmap.getByteCount());
    assertEquals(bitmap.getHeight(), newBitmap.getHeight());
    assertEquals(bitmap.getDensity(), newBitmap.getDensity());
    assertEquals(bitmap.getRowBytes(), newBitmap.getRowBytes());

    tearDown();
  }
  public void uploadImg(
      String dirType,
      String userId,
      String fileName,
      Bitmap bitmap,
      final ChatCallBack callback,
      boolean compress,
      String bucketName) {

    OSSBucket mBucket = ossService.getOssBucket(bucketName);
    OSSData ossData = ossService.getOssData(mBucket, userId + "/" + dirType + "/" + fileName);
    if (compress && bitmap.getByteCount() > 1024 * 100)
      ossData.setData(ImageUtils.bitmap2Bytes(bitmap, true), "img"); // 指定需要上传的数据和它的类型
    else ossData.setData(ImageUtils.bitmap2Bytes(bitmap, false), "img");
    ossData.enableUploadCheckMd5sum(); // 开启上传MD5校验
    ossData.uploadInBackground(
        new SaveCallback() {
          @Override
          public void onSuccess(String objectKey) {
            LogUtil.info(ChatManager.class, objectKey);
            callback.onSuccess();
            callback.onSuccess(objectKey);
          }

          @Override
          public void onProgress(String objectKey, int byteCount, int totalSize) {
            callback.onProgress(byteCount, totalSize);
          }

          @Override
          public void onFailure(String objectKey, OSSException ossException) {
            callback.onError(ChatManager.ERROR_OSS, ossException.getMessage());
          }
        });
  }
Пример #5
0
  @SuppressLint("NewApi")
  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
    Bitmap mBitmap = BitmapFactory.decodeStream(is);

    System.out.println("btm" + mBitmap.getByteCount());
    setBm(mBitmap); // 初始化全局变量

    userId = "";
    UserName = "";
    levelId = "";

    DisplayImageOptions defaultOptions =
        new DisplayImageOptions.Builder() //
            .showImageForEmptyUri(R.drawable.ic_launcher) //
            .showImageOnFail(R.drawable.ic_launcher) //
            .cacheInMemory(true) //
            .cacheOnDisk(true) //
            .build(); //
    ImageLoaderConfiguration config =
        new ImageLoaderConfiguration //
                .Builder(getApplicationContext()) //
            .defaultDisplayImageOptions(defaultOptions) //
            .discCacheSize(50 * 1024 * 1024) //
            .discCacheFileCount(10) // 缓存一百张图片
            .writeDebugLogs() //
            .build(); //
    ImageLoader.getInstance().init(config);
  }
  public static String convertBitmapToBase64(Bitmap bitmap) {
    final int size = bitmap.getByteCount();

    ByteBuffer dst = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(dst);
    return Base64.encodeToString(dst.array(), Base64.DEFAULT);
  }
Пример #7
0
  public static void cacheData(Context context, Bitmap bitmap, String name) throws IOException {

    File cacheDir = context.getCacheDir();
    long size = getDirSize(cacheDir);
    long newSize = bitmap.getByteCount() + size;

    if (newSize > MAX_SIZE) {
      cleanDir(cacheDir, newSize - MAX_SIZE);
    }

    String[] imageCheckSplit = new String[8];
    imageCheckSplit = name.split("/");

    String imageCheck = imageCheckSplit[7] + imageCheckSplit[8];

    File cacheImage = new File(cacheDir.getAbsolutePath(), imageCheck);
    OutputStream os = new FileOutputStream(cacheImage.getAbsolutePath());
    try {
      bitmap.compress(CompressFormat.JPEG, 100, os);
    } finally {
      try {
        os.flush();
        os.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Пример #8
0
 /**
  * Get the size in bytes of a bitmap.
  *
  * @param bitmap The bitmap to calculate the size of.
  * @return size of bitmap in bytes.
  */
 @TargetApi(12)
 public static int getBitmapSize(Bitmap bitmap) {
   if (Utils.hasHoneycombMR1()) {
     return bitmap.getByteCount();
   }
   // Pre HC-MR1
   return bitmap.getRowBytes() * bitmap.getHeight();
 }
Пример #9
0
 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
 public static int getBitmapSize(Bitmap bitmap) {
   if (DeviceUtils.hasHoneycombMR1()) {
     return bitmap.getByteCount();
   }
   // Pre HC-MR1
   return bitmap.getRowBytes() * bitmap.getHeight();
 }
Пример #10
0
 /**
  * Get the size in bytes of a bitmap.
  *
  * @param bitmap
  * @return size in bytes
  */
 @SuppressLint("NewApi")
 public static int getBitmapSize(Bitmap bitmap) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
     return bitmap.getByteCount();
   }
   // Pre HC-MR1
   return bitmap.getRowBytes() * bitmap.getHeight();
 }
Пример #11
0
  private static int getBitmapSize(Bitmap bitmap) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
      return bitmap.getByteCount();
    }
    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
  }
 /**
  * Get the size in bytes of a bitmap
  *
  * @param bitmap
  * @return size in bytes
  */
 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
 public static int getBitmapSize(Bitmap bitmap) {
   if (Utils.hasHoneycombMR1()) {
     return bitmap
         .getByteCount(); // Returns the number of bytes used to store this bitmap's pixels
   }
   // SDK Version prior to HoneycombMR1
   return bitmap.getRowBytes() * bitmap.getHeight();
 }
Пример #13
0
  @Test(expected = NullPointerException.class)
  @org.robolectric.annotation.Config(
      sdk = {
        Build.VERSION_CODES.JELLY_BEAN_MR1,
        Build.VERSION_CODES.JELLY_BEAN_MR2,
        Build.VERSION_CODES.KITKAT,
        Build.VERSION_CODES.LOLLIPOP
      })
  public void byteCountIsAccurate() {
    Bitmap b1 = Bitmap.createBitmap(10, 10, Config.ARGB_8888);
    assertThat(b1.getByteCount()).isEqualTo(400);
    Bitmap b2 = Bitmap.createBitmap(10, 10, Config.RGB_565);
    assertThat(b2.getByteCount()).isEqualTo(200);

    // Null config is not allowed.
    Bitmap b3 = Bitmap.createBitmap(10, 10, null);
    b3.getByteCount();
  }
Пример #14
0
 // TODO get bytes of bitmap
 private static int byteSizeOf(Bitmap bitmap) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     return bitmap.getAllocationByteCount();
   } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
     return bitmap.getByteCount();
   } else {
     return bitmap.getRowBytes() * bitmap.getHeight();
   }
 }
Пример #15
0
  @SuppressLint("NewApi")
  public static int getBitmapSize(Bitmap bitmap) {

    if (Build.VERSION.SDK_INT >= 12) {
      return bitmap.getByteCount();
    } else {
      return bitmap.getRowBytes() * bitmap.getHeight();
    }
  }
 public int getSizeInBytes() {
   if (bitmap == null) {
     return 0;
   }
   // Taken from DevBytes, "Making Apps Beautiful - Part 4" Youtube episode
   if (Build.VERSION.SDK_INT >= 12) {
     return bitmap.getByteCount();
   } else {
     return bitmap.getRowBytes() * bitmap.getHeight();
   }
 }
Пример #17
0
  @TargetApi(12)
  public static int getBitmapSize(Bitmap bitmap) {
    int size;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
      size = bitmap.getByteCount();
    } else {
      size = bitmap.getRowBytes() * bitmap.getHeight();
    }

    return size;
  }
Пример #18
0
 protected int a(Object obj, Bitmap bitmap)
 {
     int i;
     if (android.os.Build.VERSION.SDK_INT >= 19)
     {
         i = bitmap.getAllocationByteCount();
     } else
     {
         i = bitmap.getByteCount();
     }
     return (i + 1023) / 1024;
 }
 private void setBitmap(Bitmap bitmap) {
   if (mImageView != null) {
     mBitmap = bitmap;
     mImageView.setImageBitmap(mBitmap);
     int size = mBitmap.getByteCount() / 1024;
     Log.d(
         TAG,
         "bitmap size: "
             + size
             + "kb, hight: "
             + mBitmap.getHeight()
             + ", width: "
             + mBitmap.getWidth());
   } else Log.d(TAG, "the mImageView is null");
 }
Пример #20
0
    @Override
    public Response load(Uri uri, boolean localCacheOnly) throws IOException {
      Response response = null;
      String packageName = uri.getLastPathSegment();

      PackageManager pm = context.getPackageManager();
      try {
        BitmapDrawable icon = (BitmapDrawable) pm.getApplicationIcon(packageName);
        Bitmap bmp = icon.getBitmap();

        response = new Response(bmp, false, bmp.getByteCount());
      } catch (NameNotFoundException e) {
        response = null;
      }
      return response;
    }
  /**
   * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from Android 4.4 (KitKat)
   * onward this returns the allocated memory size of the bitmap which can be larger than the actual
   * bitmap data byte count (in the case it was re-used).
   *
   * @param value
   * @return size in bytes
   */
  @TargetApi(VERSION_CODES.KITKAT)
  public static int getBitmapSize(BitmapDrawable value) {
    Bitmap bitmap = value.getBitmap();

    // From KitKat onward use getAllocationByteCount() as allocated bytes can potentially be
    // larger than bitmap byte count.
    if (Utils.hasKitKat()) {
      return bitmap.getAllocationByteCount();
    }

    if (Utils.hasHoneycombMR1()) {
      return bitmap.getByteCount();
    }

    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
  }
Пример #22
0
  @Override
  public Bitmap snapshot(boolean transparent) {
    final GiCoreView v = mView.coreView();
    int doc, gs;

    synchronized (v) {
      doc = v.acquireFrontDoc();
      gs = v.acquireGraphics(mView.viewAdapter());
    }
    try {
      final LogHelper log = new LogHelper();
      final Bitmap bitmap = mView.snapshot(doc, gs, transparent);
      log.r(bitmap != null ? bitmap.getByteCount() : 0);
      return bitmap;
    } finally {
      GiCoreView.releaseDoc(doc);
      v.releaseGraphics(gs);
    }
  }
Пример #23
0
  /**
   * @param candidate - Bitmap to check
   * @param targetOptions - Options that have the out* value populated
   * @return true if <code>candidate</code> can be used for inBitmap re-use with <code>targetOptions
   *     </code>
   */
  @TargetApi(19)
  private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    // BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!Utils.hasKitKat()) {
      // On earlier versions, the dimensions must match exactly and the
      // inSampleSize must be 1
      return candidate.getWidth() == targetOptions.outWidth
          && candidate.getHeight() == targetOptions.outHeight
          && targetOptions.inSampleSize == 1;
    }

    // From Android 4.4 (KitKat) onward we can re-use if the byte size of
    // the new bitmap
    // is smaller than the reusable bitmap candidate allocation byte count.
    int width = targetOptions.outWidth / targetOptions.inSampleSize;
    int height = targetOptions.outHeight / targetOptions.inSampleSize;
    int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
    return byteCount <= candidate.getByteCount();
    // END_INCLUDE(can_use_for_inbitmap)
  }
Пример #24
0
  public void onImageAvailable(Bitmap bmp) {
    int bytes = bmp.getByteCount();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    ByteBuffer buf = ByteBuffer.allocate(bytes);
    bmp.copyPixelsToBuffer(buf);

    targetWidth = width;
    targetHeight = height;

    int stride = width * 4;
    Frame frame =
        new Frame()
            .setBuffer(buf)
            .setWidth(width)
            .setHeight(height)
            .setRowStride(stride)
            .setTs(System.currentTimeMillis());
    captureQueue.offer(frame);
  }
Пример #25
0
  private static class TextDrawing {
    private final Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ALPHA_8);
    private final Canvas canvas = new Canvas(bitmap);
    private final ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());

    public void set(String text) {
      canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
      canvas.drawText(text, 0, 50 / 2, paint);
      buffer.rewind();
      bitmap.copyPixelsToBuffer(buffer);
    }

    @Override
    public boolean equals(Object o) {
      return o != null
          && (o instanceof TextDrawing)
          && Arrays.equals(buffer.array(), ((TextDrawing) o).buffer.array());
    }

    @Override
    public int hashCode() {
      return Arrays.hashCode(buffer.array());
    }
  }
Пример #26
0
  /** @return size in bytes of the underlying bitmap */
  @SuppressLint("NewApi")
  public static int getSizeInBytes(@Nullable Bitmap bitmap) {
    if (bitmap == null) {
      return 0;
    }

    // There's a known issue in KitKat where getAllocationByteCount() can throw an NPE. This was
    // apparently fixed in MR1: http://bit.ly/1IvdRpd. So we do a version check here, and
    // catch any potential NPEs just to be safe.
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
      try {
        return bitmap.getAllocationByteCount();
      } catch (NullPointerException npe) {
        // Swallow exception and try fallbacks.
      }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
      return bitmap.getByteCount();
    }

    // Estimate for earlier platforms.
    return bitmap.getWidth() * bitmap.getRowBytes();
  }
Пример #27
0
 @Override
 public Response load(Uri uri, boolean localCacheOnly) throws IOException {
   String albumId = uri.getLastPathSegment();
   Bitmap bitmap = getAlbumArt(context, albumId);
   return (bitmap != null) ? new Response(bitmap, false, bitmap.getByteCount()) : null;
 }
Пример #28
0
 /** Get the size in bytes of a bitmap. */
 public static final int getBitmapSize(final Bitmap bitmap) {
   return bitmap.getByteCount();
 }
Пример #29
0
  /** Load a JPEG into processing */
  @SuppressLint("NewApi")
  private void loadJPEG() throws FileNotFoundException {
    // TODO Auto-generated method stub

    Log.d(TAG, "before LoadJPEG");
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.input);

    if (bmp != null && bmp.getConfig() == Bitmap.Config.ARGB_8888) {
      Log.d(TAG, "Input image is valid and ARGB_8888");

      /** Preparing image size */
      int bytes = bmp.getByteCount();
      Log.d(TAG, "bmp size " + bytes);
      float unwarpedAspectRatio = (float) (360.0 / 55.0);
      int unwarpedW = 1280;
      int unwarpedH = (int) (unwarpedW / unwarpedAspectRatio);

      /** Preparing INPUT - Convert bitmap --> byte array WITHOUT compression */
      ByteBuffer buffer = ByteBuffer.allocate(bytes);
      bmp.copyPixelsToBuffer(buffer);
      Log.d(TAG, "byteArrayData size " + buffer.array().length);
      byte[] input = buffer.array();

      /** Preparing OUTPUT byte array - Initialize output */
      byte[] OutputByteArray = new byte[unwarpedW * unwarpedH * 4];
      Log.d(TAG, "output int [] size " + OutputByteArray.length);

      /** Processing Unwrapping */
      FastCVUtil.init();
      unwarp(input, bmp.getWidth(), bmp.getHeight(), OutputByteArray);
      Log.d(TAG, "after process output int [] size " + OutputByteArray.length);

      /** Convert OutputByteArray byte[] to bitmap */
      /** !!decodeByteArray is for compressed bitmap, our image is uncompress */
      // Bitmap outDisplay = BitmapFactory.decodeByteArray(OutputByteArray, 0,
      // OutputByteArray.length);

      /** Try Native to display bitmap */
      Bitmap.Config conf = Bitmap.Config.ARGB_8888;
      Bitmap OutputBitmap = Bitmap.createBitmap(unwarpedW, unwarpedH, conf);
      byteArraytoBitmap(OutputBitmap, OutputByteArray);

      /** Store bitmap to sdcard */
      try {
        OutputStream stream = new FileOutputStream("/sdcard/unwarped.jpg");
        OutputBitmap.compress(CompressFormat.JPEG, 100, stream);
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      Log.d(
          TAG,
          "Remap "
              + unwarpedW
              + " x "
              + unwarpedH
              + " process time (ms): "
              + FastCVUtil.getFastCVProcessTime() / 1000);
    }
  }
Пример #30
0
 public static int getSizeInBytes(Bitmap bitmap) {
   return bitmap.getByteCount();
 }