Skip to content Skip to sidebar Skip to footer

Cwac Camera: Why My Simplecamerahost Saveimage Is So Slow, Am I Doing Something Wrong?

How to optimize this peace of code? It takes about a minute on saveImage method. class ObrolSimpleHost extends SimpleCameraHost { private final String[] SCAN_TYPES = {'image/webp

Solution 1:

Here is my own answer.

Fixed issues which CommonsWare mentioned and resize bitmap before compression by createScaledBitmap:

classObrolSimpleHostextendsSimpleCameraHost {
  privatefinal String[] SCAN_TYPES = {"image/" + imputType};
  privateContextcontext=null;

  publicObrolSimpleHost(Context _ctxt) {
    super(_ctxt);
    this.context = getActivity();
  }

  @OverridepublicvoidsaveImage(PictureTransaction xact, Bitmap bitmap) {
    Filephoto= getPhotoPath();
    Stringpath= photo.getPath().replace("jpg", imputType);
    if (photo.exists()) {
      photo.delete();
    }

    /**
     * Resizing bitmap, so save some ms in compression
     * http://stackoverflow.com/questions/17839388/creating-a-scaled-bitmap-with-createscaledbitmap-in-android
     */finalintmaxSize=960;
    int outWidth;
    int outHeight;
    intinWidth= bitmap.getWidth();
    intinHeight= bitmap.getHeight();
    if(inWidth > inHeight){
      outWidth = maxSize;
      outHeight = (inHeight * maxSize) / inWidth;
    } else {
      outHeight = maxSize;
      outWidth = (inWidth * maxSize) / inHeight;
    }
    BitmapresizedBitmap= Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);

    try {
      FileOutputStreamfos=newFileOutputStream(path);
      if (imputType.equals("jpeg")) {
        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
      } else {
        resizedBitmap.compress(Bitmap.CompressFormat.WEBP, 70, fos);
      }
      fos.flush();
      fos.getFD().sync();
      if (scanSavedImage()) {
        MediaScannerConnection.scanFile(context, newString[]{photo.getPath()}, SCAN_TYPES, null);
      }
    } catch (java.io.IOException e) {
      handleException(e);
    }
    EventBus.getDefault().postSticky(newEvents.PreparingBitmapEvent(path));
    getActivity().finish();
  }

  @OverridepublicvoidsaveImage(PictureTransaction xact, byte[] image) {
    // do nothing
  }
}

Instead of standard bitmap use other library which build with JNI

In my case I am going to try Roid-WebP

Post a Comment for "Cwac Camera: Why My Simplecamerahost Saveimage Is So Slow, Am I Doing Something Wrong?"