在不使用位图的情况下裁剪相机拍摄的图像

Crop Image taken by camera without using Bitmap

我正在开发一个图像处理应用程序,我需要将拍摄的照片分成四个区域,但是使用 BitmapFactoy 会占用太多资源和时间并且会减慢我的速度,我想使用原始字节 [] 数据来完成此操作public void onPictureTaken(byte[] data, Camera camera) 给了我。 现在这就是我所做的,我想改进它以使用 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();
    }