Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 合并位图时如何忽略像素?_Java_Android - Fatal编程技术网

Java 合并位图时如何忽略像素?

Java 合并位图时如何忽略像素?,java,android,Java,Android,这是我的合并代码 private Bitmap bitmapOverlayToCenter(Bitmap bitmap1, Bitmap overlayBitmap) { int bitmap1Width = bitmap1.getWidth(); int bitmap1Height = bitmap1.getHeight(); int bitmap2Width = overlayBitmap.getWidth(); int bit

这是我的合并代码

private Bitmap bitmapOverlayToCenter(Bitmap bitmap1, Bitmap overlayBitmap) {
        int bitmap1Width = bitmap1.getWidth();
        int bitmap1Height = bitmap1.getHeight();
        int bitmap2Width = overlayBitmap.getWidth();
        int bitmap2Height = overlayBitmap.getHeight();

        float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
        float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);

        Bitmap finalBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmap1.getConfig());
        Canvas canvas = new Canvas(finalBitmap);
        canvas.drawBitmap(bitmap1, new Matrix(), null);
        canvas.drawBitmap(overlayBitmap, marginLeft, marginTop, null);
        return finalBitmap;
    }
但我想合并它而不覆盖黑色像素。
注意:qr码图像不透明,绿色图像在qr后面不可见最简单的方法是在绘制叠加(绿色)位图时使用油漆,但我不确定要将哪个参数用作PorterDuff.Mode():

但是,如果您已经尝试了所有值,但没有成功,那么您必须在绘制此最新值之前从覆盖图(绿色)中减去黑色像素。 步骤:

  • 从QRCode位图中删除所有白色像素
  • 将剩余的黑色像素减去绿色覆盖(这样,绿色区域内部将有孔)
  • 像往常一样绘制绿色覆盖图

  • 这就是我通过在背景上添加黑色像素然后合并图像来解决问题的方法

            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix bitMatrix = writer.encode(message, BarcodeFormat.QR_CODE, 1024, 1024, null);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            Bitmap backgroundBmp = ImageUtils.convertToBitmap(
                    ResourcesCompat.getDrawable(getResources(), getQrBackground(), null),
                    width,
                    height
            );
            final Bitmap qrBmp = Bitmap.createBitmap(width, height, 
            Bitmap.Config.RGB_565);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    boolean isBlack = bitMatrix.get(x, y);
                    qrBmp.setPixel(x, y, isBlack ? Color.BLACK : Color.WHITE);
                    // add black pixel on background
                    if (isBlack) {
                        backgroundBmp.setPixel(x, y, Color.BLACK);
                    }
                }
            }
            return ImageUtils.mergeToPin(qrBmp, backgroundBmp);
    
    QRCodeWriter writer=新的QRCodeWriter();
    BitMatrix BitMatrix=writer.encode(消息,条形码格式.QR_码,1024,1024,空);
    int-width=bitMatrix.getWidth();
    int height=bitMatrix.getHeight();
    位图背景bmp=ImageUtils.convertToBitmap(
    ResourcesCompat.getDrawable(getResources(),getQrBackground(),null),
    宽度,
    高度
    );
    最终位图qrBmp=Bitmap.createBitmap(宽度、高度、,
    Bitmap.Config.RGB_565);
    对于(int x=0;x
    是!谢谢,当二维码生成时,我会在同一位置向背景图像添加黑色像素,然后合并图像
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix bitMatrix = writer.encode(message, BarcodeFormat.QR_CODE, 1024, 1024, null);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            Bitmap backgroundBmp = ImageUtils.convertToBitmap(
                    ResourcesCompat.getDrawable(getResources(), getQrBackground(), null),
                    width,
                    height
            );
            final Bitmap qrBmp = Bitmap.createBitmap(width, height, 
            Bitmap.Config.RGB_565);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    boolean isBlack = bitMatrix.get(x, y);
                    qrBmp.setPixel(x, y, isBlack ? Color.BLACK : Color.WHITE);
                    // add black pixel on background
                    if (isBlack) {
                        backgroundBmp.setPixel(x, y, Color.BLACK);
                    }
                }
            }
            return ImageUtils.mergeToPin(qrBmp, backgroundBmp);