我试图从相机意图捕获图像,然后在其上应用图像过滤器。详细说明图像将是由相机捕获的图像,并且图像过滤器将作为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:我能够做出与乔鲁提供了文章的帮助下取得了一些进展。问题似乎是混合了2个位图。 drawBitmap方法只会在我有的情况下绘制一个位图。下面的代码行实际上会尝试混合2个位图。我还附上了一张描述我进步的图片。底层的位图现在是明显更加明显:
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
我仍然玩弄了一段时间,实现期望的输出之前。
谢谢你的回应。我已经确保这两个图像都是ARGB_8888格式。看起来过滤器正在失去一些透明度,我不知道为什么。 – Abhijit 2012-01-08 23:32:12
阅读http://android.nakatome.net/2010/04/bitmap-basics.html可能会有所帮助。 – Joru 2012-01-09 10:14:10
这篇文章是非常有帮助的。我已将其中提出的一些想法注入到我的代码中,并显示出正面但不完整的结果。所以,我会继续努力并在解决问题后立即发布解决方案。再次感谢 - 我接受了你的回答,因为你指出了我的正确方向。 – Abhijit 2012-01-09 17:06:35