Android 不使用位图裁剪相机拍摄的图像
我正在开发一个图像处理应用程序,我需要将拍摄的照片分为四个区域,但使用BitMapFactory会占用太多资源和时间,速度会变慢。我希望使用Android 不使用位图裁剪相机拍摄的图像,android,android-camera,Android,Android Camera,我正在开发一个图像处理应用程序,我需要将拍摄的照片分为四个区域,但使用BitMapFactory会占用太多资源和时间,速度会变慢。我希望使用public void onPictureTaken(byte[]data,Camera Camera)提供给我的原始byte[]数据执行此操作。 现在我就是这么做的,我想改进它,使用byte[]来加速它: public void onPictureTaken(byte[] data, Camera camera) { new Asy
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;
}
此外,如果您能给我一些建议,以提高这些操作的速度,我将不胜感激。您是否考虑过(目前只支持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();
}
使用Traceview确定问题所在。我认为堆空间将是一个比CPU速度更大的问题。谢谢你提到commons这个家伙。我知道堆空间是一个问题,但在未来的版本中,堆问题将得到解决,因为我现在只使用位图进行预览,它们将在将来被删除,但因为我需要在“getDominantColor(…)中检查图片的每个像素点。”它将永远减慢速度,我的首要任务是通过避免使用位图来减少时间。@Commonware有没有一种方法可以在不使用位图的情况下裁剪我的图像,以加快图像的处理速度以供将来处理?当您运行Traceview时,为了确定问题所在,你学到了什么?@commonware几乎什么都没有我确实看过,但我想它对我来说太复杂了。但根据我的计算,没有其他东西会减慢应用程序的速度,因为当我压缩图像时,操作速度会以良好的方式提高,问题出现的地方是我与pixles交互的地方,即“getDominiantColor()”,还将原始字节[]转换为位图,并再次在中将位图转换为int[]“getDominiantColor()”我正在获取字节[]从照相机,除了我应该如何确定Rect的大小之外?使用
BitmapFactory
和options.inJustDecodeBounds=true;
获得位图的尺寸,然后计算矩形。很抱歉,我完全忘记了这一点,但我的用户实际上使用的是android 2.3手机,我必须支持android 2.3。谢谢你的回复,但我真的很抱歉ed支持姜饼。@ErfanMowlaei Gingerbread是api级别10(2.3.3~2.3.7)中的最新版本,您可以在姜饼中使用此api。@alijandro无需提供图像路径即可进行裁剪,我的意思是直接从原始字节[]?(onPictureTaken(字节[]数据,摄像头))