android view 叠加滤镜,android – 将叠加(图像滤镜)应用于位图

我试图从相机意图捕获图像,然后在其上应用图像过滤器.详细说明图像将是由相机捕获的图像,并且图像过滤器将作为png文件在资源中可用.我可以将滤镜叠加在原始图像的顶部.但是,一旦覆盖,原始图像“几乎”不可见(这意味着过滤器实际上堆叠在原始图像上而不仅仅是替换它).我有几张图片来说明我的问题.第一张图片是在Photoshop中 – 当我在图像上面放置一个滤镜时,它似乎很好.第二个图像是由下面引用的代码生成的 – 您可以清楚地看到滤镜效果丢失.有人会知道为什么会发生这样的事情.我在这里错过了一些逻辑吗?

以下是我的代码.如果您发现此处缺少任何最佳做法,我深表歉意.我最初尝试测试代码:

mPictureView = (ImageView) findViewById(R.id.pictureView);

filterButton = (Button) findViewById(R.id.filter_button1);

// define the threshold fro scaling the image

private final double SCALE_THRESHOLD = 6.0;

// acquire the bitmap (photo captured) from the Camera Intent - the uri is

// passed from a previous activity that accesses the camera and the current

// activity is used to display the bitmap

Uri imageUri = getIntent().getData();

Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

// set the imageView in the current activity to display the picture retrieved

// from the camera

mPictureView.setImageBitmap(imageBitmap);

// get the dimensions of the original bitmap

int photoWidth = imageBitmap.getWidth();

int photoHeight = imageBitmap.getHeight();

filterButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// set the options

Options options = new BitmapFactory.Options();

options.inScaled = false;

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

// get the image (png file) filter from resources using the options

Bitmap filter = BitmapFactory.decodeResource(getResources(), R.drawable.colorful_filter,options);

// create a scaled copy of the filter

Bitmap filtercopy = Bitmap.createScaledBitmap(filter, (int)(photoWidth/SCALE_THRESHOLD,(int)(photoHeight/SCALE_THRESHOLD), true);

// recycle the used bitmap

filter.recycle();

filter = null;

// get a scaled, mutable copy of the orginial image

Bitmap imagecopy = Bitmap.createScaledBitmap(imageBitmap,(int)(photoWidth/SCALE_THRESHOLD), (int)(photoHeight/SCALE_THRESHOLD),true);

// recycle the used bitmap

imageBitmap.recycle();

imageBitmap = null;

Paint paint = new Paint();

paint.setAntiAlias(true);

//paint.setAlpha(230); - if a discrete value is set, then the image beneath

// the filter is visible. But, I don't understand why I need to do this.

// Besides, that reduces the efficacy of the filter

// create a canvas with the original image as the underlying image

Canvas canvas = new Canvas(imagecopy);

// now, draw the filter on top of the bitmap

canvas.drawBitmap(filtercopy, 0, 0, paint);

// recycle the used bitmap

filtercopy.recycle();

filtercopy = null;

//set the filtered bitmap as the image

mPictureView.setImageBitmap(imagecopy);

}

编辑1:在Joru提供的文章的帮助下,我能够取得一些进展.问题似乎是混合2位图.在我拥有的情况下,drawBitmap方法只会在另一个上绘制一个位图.以下代码行实际上将尝试混合2位图.我还附上了描绘我进步的图像.现在,基础位图显然更加明显:

paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));

在达到所需的输出之前,我还需要玩一段时间.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值