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

标签 java android android-layout bitmap camera

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

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

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

enter image description here

最佳答案

花了一个下午的时间,最后有解决方案,结果比我想象的要简单:

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/

关于java - 如何在android中裁剪位图的重叠区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19419664/

相关文章:

java - 为什么arraylist 类既要实现List 又要扩展AbstractList?

java - 在 Android 中按下按钮时如何设置单选按钮的文本颜色?

android - 如何对各种 TextView 进行比例缩放

java - 如何从 ContainerRequestContext 获取路径参数

Java 无法使 TempListener 工作

java - 使用 Retrofit Android 发送 ArrayList<String> 作为一部分

java - 如何以编程方式或使用 xml 属性删除回收器分隔符/分隔符

android - Sound Pool 只播放一次声音

android - 输入类型无法正常工作

java - 自定义布局中 switchpreference 中开/关切换的颜色变化