如何优化这段代码?
saveImage
方法需要大约一分钟。
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
}
}
我从Camera Fragment打电话给ObrolSimpleHost
:
PictureTransaction xact = new PictureTransaction(getHost());
xact.needBitmap(true);
takePicture(xact);
答案 0 :(得分:0)
这是我自己的答案。
CommonsWare
压缩之前bitmap
提及并调整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
}
}
bitmap
使用其他使用JNI 在我的情况下,我将尝试Roid-WebP