应用覆盖(图像过滤器),以一个位图位图、过滤器、图像

2023-09-05 07:40:42 作者:初见,一眼足以

我试图捕捉从相机意图的图像,然后将其应用于图像过滤器。为了详细描述该图像将是由摄像机和图像过滤器将是可用的资源作为一个PNG文件中捕获的图像。我能够覆盖在过滤器上的原始图像的顶​​部。但是,一旦叠加编原始图像是几乎不可见的(这意味着该滤波器逸岸堆叠在原始图像上,而不是仅仅更换它)。我有一对夫妇的图像来说明我的问题。第一个图像是在Photoshop中 - 当我把一个过滤器上的图像的顶部,它似乎只是罚款。第二个图像是由code生产举例如下 - 你可以清楚地看到,滤镜效果将丢失。会有人有线索,为什么这样的事情发生。我失去了一些逻辑吗?

I am trying to capture an image from the camera intent and then apply an image filter on it. To elaborate the image would be an image captured by the camera and the image filter would be available in the resources as a png file. I am able to overlay the filter on top of the original image. But, once overlay-ed the original image is 'almost' invisible (which means that the filter is infact being stacked on the original image and not merely replacing it). I have a couple of images to illustrate my problem. The first image was in Photoshop - when I placed a filter on top of an image, it seemed just fine. The second image is the produced by the code cited below - you can clearly see that the filter effect is missing. Would someone have a clue as to why something like this is occurring. Am I missing some logic here?

以下为code,我有。如果您发现这里缺少任何最佳做法,我表示歉意。我最初是想测试code:

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将只是画一个位图比其他的,我有这种情况。的code以下行实际上将尝试进行混合的2位图。我还附上描绘我的进步的图像。底层的位图是明显更加明显现在:

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

我仍然玩弄了一段时间,达到理想的输出之前。

I have to still play around with it for some time, before achieving the desired output.

推荐答案

您可以尝试的几件事情:

You could try a few things:

Bitmap new = old.copy(Config.ARGB_8888, true);

要确保您正在打开从 MediaStore 的位图是这种格式。如果不是,那很可能造成您的问题。

To make sure the bitmap you are opening from MediaStore is in that format. If it isn't, that is likely to cause your problem.

 
精彩推荐
图片推荐