CWAC摄像头:为什么我的SimpleCameraHost saveImage很慢,我是不是做错了什么? [英] CWAC Camera: why my SimpleCameraHost saveImage is so slow, am I doing something wrong?

查看:455
本文介绍了CWAC摄像头:为什么我的SimpleCameraHost saveImage很慢,我是不是做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何优化code的这一和平?
这需要有关 saveImage一分钟方法。

 类ObrolSimpleHost扩展SimpleCameraHost {
  私人最终的String [] = SCAN_TYPES {图像/ WEBP};
  私人上下文的背景下= NULL;  公共ObrolSimpleHost(上下文_ctxt){
    超(_ctxt);
    this.context = getActivity();
  }  @覆盖公共无效saveImage(PictureTransaction XACT,位图位图){
    文件照片= getPhotoPath();
    如果(photo.exists()){
      photo.delete();
    }
    尝试{
      FOS的FileOutputStream =新的FileOutputStream(photo.getPath());
      bitmap.com preSS(Bitmap.Com pressFormat.WEBP,70,FOS);
      fos.flush();
      fos.getFD()同步()。
      如果(scanSavedImage()){
        MediaScannerConnection.scanFile(背景下,新的String [] {photo.getPath()},SCAN_TYPES,NULL);
      }
    }赶上(java.io.IOException异常五){
      handleException(E);
    }
  }  @覆盖公共无效saveImage(PictureTransaction XACT,字节[]图像){
    // 没做什么
  }
}

我打电话 ObrolSimpleHost 从CameraFragment:

  PictureTransaction XACT =新PictureTransaction(和getHost());
xact.needBitmap(真);
takePicture(XACT);


解决方案

下面是我自己的答案。

其中 CommonsWare 提到和调整

问题修正位图之前通过 COM pression createScaledBitmap

 类ObrolSimpleHost扩展SimpleCameraHost {
  私人最终的String [] = SCAN_TYPES {图像/+ imputType};
  私人上下文的背景下= NULL;  公共ObrolSimpleHost(上下文_ctxt){
    超(_ctxt);
    this.context = getActivity();
  }  @覆盖公共无效saveImage(PictureTransaction XACT,位图位图){
    文件照片= getPhotoPath();
    字符串路径= photo.getPath()代替(JPG,imputType)。
    如果(photo.exists()){
      photo.delete();
    }    / **
     *调整大小的位图,所以保存COM pression一些毫秒
     * http://stackoverflow.com/questions/17839388/creating-a-scaled-bitmap-with-createscaledbitmap-in-android
     * /
    最终诠释MAXSIZE = 960;
    INT outWidth;
    INT outHeight;
    INT inWidth = bitmap.getWidth();
    INT inHeight = bitmap.getHeight();
    如果(inWidth> inHeight){
      outWidth = MAXSIZE;
      outHeight =(inHeight * MAXSIZE)/ inWidth;
    }其他{
      outHeight = MAXSIZE;
      outWidth =(inWidth * MAXSIZE)/ inHeight;
    }
    位图resizedBitmap = Bitmap.createScaledBitmap(位图,outWidth,outHeight,FALSE);    尝试{
      FOS的FileOutputStream =新的FileOutputStream(路径);
      如果(imputType.equals(JPEG)){
        resizedBitmap.com preSS(Bitmap.Com pressFormat.JPEG,70,FOS);
      }其他{
        resizedBitmap.com preSS(Bitmap.Com pressFormat.WEBP,70,FOS);
      }
      fos.flush();
      fos.getFD()同步()。
      如果(scanSavedImage()){
        MediaScannerConnection.scanFile(背景下,新的String [] {photo.getPath()},SCAN_TYPES,NULL);
      }
    }赶上(java.io.IOException异常五){
      handleException(E);
    }
    。EventBus.getDefault()postSticky(新活动preparingBitmapEvent(路径)。);
    getActivity()完成()。
  }  @覆盖公共无效saveImage(PictureTransaction XACT,字节[]图像){
    // 没做什么
  }
}

除了标准的位图的使用符合的 JNI

在我的情况,我要尝试固醇-的WebP

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"};
  private Context context = null;

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

  @Override public void saveImage(PictureTransaction xact, Bitmap bitmap) {
    File photo = getPhotoPath();
    if (photo.exists()) {
      photo.delete();
    }
    try {
      FileOutputStream fos = new FileOutputStream(photo.getPath());
      bitmap.compress(Bitmap.CompressFormat.WEBP, 70, fos);
      fos.flush();
      fos.getFD().sync();
      if (scanSavedImage()) {
        MediaScannerConnection.scanFile(context, new String[]{photo.getPath()}, SCAN_TYPES, null);
      }
    } catch (java.io.IOException e) {
      handleException(e);
    }
  }

  @Override public void saveImage(PictureTransaction xact, byte[] image) {
    // do nothing
  }
}

I am calling ObrolSimpleHost from CameraFragment:

PictureTransaction xact = new PictureTransaction(getHost());
xact.needBitmap(true);
takePicture(xact);

解决方案

Here is my own answer.

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

class ObrolSimpleHost extends SimpleCameraHost {
  private final String[] SCAN_TYPES = {"image/" + imputType};
  private Context context = null;

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

  @Override public void saveImage(PictureTransaction xact, Bitmap bitmap) {
    File photo = getPhotoPath();
    String path = 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
     */
    final int maxSize = 960;
    int outWidth;
    int outHeight;
    int inWidth = bitmap.getWidth();
    int inHeight = bitmap.getHeight();
    if(inWidth > inHeight){
      outWidth = maxSize;
      outHeight = (inHeight * maxSize) / inWidth;
    } else {
      outHeight = maxSize;
      outWidth = (inWidth * maxSize) / inHeight;
    }
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);

    try {
      FileOutputStream fos = new FileOutputStream(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, new String[]{photo.getPath()}, SCAN_TYPES, null);
      }
    } catch (java.io.IOException e) {
      handleException(e);
    }
    EventBus.getDefault().postSticky(new Events.PreparingBitmapEvent(path));
    getActivity().finish();
  }

  @Override public void saveImage(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

这篇关于CWAC摄像头:为什么我的SimpleCameraHost saveImage很慢,我是不是做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆