2

目前,我在相机表面视图上有一个上部叠加图像。

我想做的只是输出与上层重叠的照片部分。到目前为止,我可以合并两个图像,这意味着重叠层位于相机视图的顶部。但是我怎样才能得到只有覆盖部分在输出中呢?谢谢

    public void onPictureTaken(byte[] data, Camera camera) {
        Bitmap resizedBottom;
        Bitmap resizedTop;

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 8;
        Bitmap photo=BitmapFactory.decodeByteArray(data, 0, data.length,options);

        Matrix matrix = new Matrix();
        matrix.postRotate(passDegree);

        Bitmap rotatedBitmap = Bitmap.createBitmap(photo, 0, 0,photo.getWidth(), photo.getHeight(), matrix, true);
        photo.recycle();
        photo = null;

        if (h >= w) {
            h = w;
        } else {
            w = h;
        }

        if (getResources().getConfiguration().orientation ==  Configuration.ORIENTATION_LANDSCAPE) {
            resizedBottom = Bitmap.createScaledBitmap(rotatedBitmap,w,h,false);
            resizedTop = Bitmap.createScaledBitmap(topLayer,w,h,false);
        } else {
            resizedBottom = Bitmap.createScaledBitmap(rotatedBitmap,h,w,false);
            resizedTop = Bitmap.createScaledBitmap(topLayer,h,w,false);
        }

        Canvas c = new Canvas(resizedBottom);
        Resources res = getResources();

        Drawable drawable1 = new BitmapDrawable(res,resizedBottom);
        Drawable drawable2 = new BitmapDrawable(res,resizedTop);

        if (res.getConfiguration().orientation ==  Configuration.ORIENTATION_LANDSCAPE) {
            drawable1.setBounds(0, 0, w, h);
            drawable2.setBounds(0, 0, w, h);
        } else {
            drawable1.setBounds(0, 0, h, w);
            drawable2.setBounds(0, 0, h, w);
        }

        drawable1.draw(c);
        drawable2.draw(c);

        File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        // Write to SD Card
        String fileName = String.format(sdDir+"/%d.jpg", System.currentTimeMillis());
        OutputStream os = null;
        try {
            os = new FileOutputStream(fileName);
            resizedBottom.compress(Bitmap.CompressFormat.PNG, 50, os);
        } catch(IOException e) {
            e.printStackTrace();
        }           
        Log.i("test", "onPictureTaken - jpeg");
        resetCam();
    }

在此处输入图像描述

4

1 回答 1

3

花了一个下午,最后有解决方案,结果比我想象的要容易:

Just
1.Create a temp bitmap to store render result
2.Set PorterDuff.Mode.DST_IN on top layer(In this mode only the overlap area is drawn)

示例代码:

Bitmap result = Bitmap.createBitmap(topLayer.getWidth(), topLayer.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(resizedBottom, 0, 0, null);
mCanvas.drawBitmap(topLayer, 0, 0, paint);
paint.setXfermode(null);

关于 PorterDuff 模式的进一步阅读:http: //www.ibm.com/developerworks/java/library/j-mer0918/

于 2013-10-23T11:00:38.363 回答