Java 如何在android中裁剪位图的重叠区域?

Java 如何在android中裁剪位图的重叠区域?,java,android,android-layout,bitmap,camera,Java,Android,Android Layout,Bitmap,Camera,目前,我在相机表面视图上有一个上部覆盖图像 我想做的是只输出与上层重叠的部分照片。到目前为止,我可以合并两个图像,这意味着,重叠层位于相机视图的顶部。但是我如何才能只得到输出中的覆盖部分呢?谢谢 public void onPictureTaken(byte[] data, Camera camera) { Bitmap resizedBottom; Bitmap resizedTop; BitmapFactory.Options opt

目前,我在相机表面视图上有一个上部覆盖图像

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

    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();
    }

花了一个下午,终于有了解决办法,结果比我想象的要容易:

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模式:

使用此-Bitmap.createBitmap(getDrawingCache(),x,y,wdth,height)这可能会使您仅获取图片区域