Dealing with large bitmap onPictureTaken android

房东的猫 提交于 2019-12-08 18:02:26

Update: Since you're still saying that the method takes too long time I would define a callback interface

interface BitmapCallback {
    onBitmapSaveComplete(Bitmap bitmap, int orientation);

    onBitmapRotateAndBWComlete(Bitmap bitmap);
}

Let your activity implement the above interface and convert the byte[] to bitmap in top of your saveBitmap method and fire the callback, before the first call to save. Rotate the imageView based on the orientation parameter and set a black/white filter on the imageView to fool the user into thinking that the bitmap is black and white (do this in your activity). See to that the calls are done on main thread (the calls to imageView). Keep your old method as you have it. (all steps need to be done anyway) Something like:

public static boolean saveBitmap(byte[] bitmapData, int orientation, String imagePath, String grayScalePath, BitmapCallback callback) throws Exception {
Boolean rotationSuccess = false;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap originalBm = null;
Bitmap bitmapRotate = null;
Bitmap grayScale = null;
FileOutputStream outStream = null;
try {
    // TODO: convert byte to Bitmap, see to that the image is not larger than your wanted size (1024z768)
    callback.onBitmapSaveComplete(bitmap, orientation);

    // save directly from byte[] to file
    saveBitmap(bitmapData, imagePath);
    .
    .
    // same as old 
    .
    .
    saveBitmap(grayScale, grayScalePath);
    // conversion done callback with the real fixed bitmap
    callback.onBitmapRotateAndBWComlete(grayScale);

    grayScale.recycle();
    grayScale = null;

    bitmapRotate.recycle();
    bitmapRotate = null;
    rotationSuccess = true;
  1. How do you setup your camera? What might be causing the long execution time in the first saveBitmap call, could be that you are using the default camera picture size settings and not reading the supported camera picture size and choosing best fit for your 1024x768 image needs. You might be taking big mpixel images and saving such, but in the end need you need < 1 mpixles (1024x768). Something like this in code:

    Camera camera = Camera.open();
    Parameters params = camera.getParameters();
    List sizes = params.getSupportedPictureSizes();
    // Loop camera sizes and find best match, larger than 1024x768

    This is probably where you will save most of the time if you are not doing this already. And do it only once, during some initialization phase.

  2. Increase the buffer to 8k in saveBitmap, change the 1024*4 to 1024*8, this would increase the performance at least, not save any significant time perhaps.

  3. To save/reuse bitmap memory consider using inBitmap field, if you have a post honeycomb version, of BitmapFactory.Options and set that field to point to bitmapRotate bitmap and send options down to your convertToGrayscale method to not need allocating yet another bitmap down in that method. Read about inBitmap here: inBitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!