我正在开发一个图像处理应用程序,我需要将拍摄的照片分为四个区域,但使用BitmapFactoy占用了太多资源和时间,速度也变慢了,我想使用public void onPictureTaken(byte[] data, Camera camera)
提供的原始byte[]数据来完成这项工作。现在这就是我所做的,我想改进它,使用byte[]来加快速度:
public void onPictureTaken(byte[] data, Camera camera) {
new AsyncImageAnalyzer(data).execute();
data = null;
}
public AsyncImageAnalyzer(byte[] d) {
mData = d;
surfaceView = null;
}
@Override
protected Void doInBackground(Void... params) {
FileOutputStream fos2 = null;
try {
String fileName = Long.toString(Calendar.getInstance().getTimeInMillis())+".jpg";
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(/*"/sdcard/XXX"*/Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"XXX",
fileName);
/*file.createNewFile();*/
fos2 = new FileOutputStream(file);
fos2.write(mData,0,mData.length);
fos2.flush();
fos2.close();
/*fos2.write(mData);*/
/*fos2.close();*/
if(extrasReceived.equals("1")){
spe.putString("FirstPicPath","/"+fileName).commit();
}else {
spe.putString("SecondPicPath","/"+fileName).commit();
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ISA exception",e.getMessage() );
}finally {
try {
if (fos2 != null) {
fos2.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sp.getInt("compression_ratio",1);
bitmap = BitmapFactory.decodeByteArray(mData, 0, mData.length,options);
mData = null;
t1G = Bitmap.createBitmap(bitmap, 0, (bitmap.getHeight() / 2) - 1, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
t2G = Bitmap.createBitmap(bitmap, (bitmap.getWidth() / 2) - 1, (bitmap.getHeight() / 2) - 1, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
t1R = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
t2R = Bitmap.createBitmap(bitmap, (bitmap.getWidth() / 2) - 1, 0, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
try {
t2RtoInt = getDominantColor(t2R);
t1RtoInt = getDominantColor(t1R);
t1GtoInt = getDominantColor(t1G);
t2GtoInt = getDominantColor(t2G);
} catch (Exception e) {
e.printStackTrace();
Log.e("exception", e.getMessage());
}
return null;
}
此外,如果能为我提供任何能加快这些行动的建议,我们将不胜感激。
你有没有想过BitmapRegionDecoder(目前只支持JPEG和PNG格式)?如果您使用API 10级及以上版本开发应用程序,您可以使用此API来加速大型位图处理。
boolean isShareable = false;
try {
InputStream is = new FileInputStream("/mnt/sdcard/test.png");
Rect rect = new Rect();
BitmapFactory.Options options = new BitmapFactory.Options();
BitmapRegionDecoder.newInstance(is, isShareable).decodeRegion(rect, options);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}