The first line of nonsense: UseopenCVSo let’s just implement it brieflyScratch musicThe effect

One, achieve the effect

Scratch the cover and award pictures

Implementation effect

Step analysis

1. Select the scratch-off cover and ash it, and add it to UIImageView.

UIImageView add UIPanGestureRecognizer gesture.

3. Monitor CGPoint position of gesture, and carry out pixel replacement of award picture within the agreed scope of cover touch.

Third, external callWSLScratchScratch class

/ / scratch music show view UIView * scratchView = [[UIView alloc] initWithFrame: CGRectMake (0, 200, the self. The frame. The size, width, self.view.frame.size.width)]; [self.view addSubview:scratchView]; NSString * bundleImage = [[NSBundle mainBundle] pathForResource:@"ydj" ofType:@"jpeg"]; UIImage * image = [UIImage imageWithContentsOfFile:bundleImage]; self.scratch = [[WSLScratch alloc] init]; [self. Scratch scratchWithImage:image showView:scratchView];Copy the code

Four,WSLScratchScratch class code

1, WSLScratch. H
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface WSLScratch : NSObject showView - (void)scratchWithImage (UIImage *)image showView (UIView *)showView; @endCopy the code
2, WSLScratch. M
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#import <opencv2/imgcodecs/ios.h> // Mat 和 UIImage互转
#endif

#import "WSLScratch.h"

//命名空间
using namespace cv;

@interface WSLScratch()
{
    Mat _originalImage;
    Mat _scratchCover;
}

@end

@implementation WSLScratch

- (void)scratchWithImage:(UIImage *)image showView:(UIView *)showView
{
    if (!image || !showView) {
        return;
    }
    //保存原图
    UIImageToMat(image, _originalImage);
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:showView.bounds];
    [showView addSubview:imageView];
    //封面
    NSString * bundleImage = [[NSBundle mainBundle] pathForResource:@"gxfc" ofType:@"jpeg"];
    UIImage * scratchCoverImage = [UIImage imageWithContentsOfFile:bundleImage];
    imageView.image = [self scratchCoverImage:scratchCoverImage];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //手势处理
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector(panAction:)];
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:pan];
}

//刮刮乐封置灰处理
- (UIImage *)scratchCoverImage:(UIImage *)image
{
    Mat img;
    UIImageToMat(image, img);
    Mat showImg(cvRound(img.rows), cvRound(img.cols), CV_8UC1 );
    //置灰处理
    cvtColor(img, showImg, COLOR_BGR2GRAY);
    //保存灰度处理矩阵
    _scratchCover = showImg;
    UIImage * showImage = MatToUIImage(_scratchCover);
    return showImage;
}

//刮一刮
- (UIImage *)beginScratchWithPoint:(CGPoint)point onView:(UIView *)onView
{
    Mat showImg(cvRound(_originalImage.rows), cvRound(_originalImage.cols), CV_8UC1 );
    cvtColor(_originalImage, showImg, COLOR_BGR2RGB);
    Mat scratchShowImg(cvRound(_scratchCover.rows), cvRound(_scratchCover.cols), CV_8UC1 );

    cvtColor(_scratchCover, scratchShowImg, COLOR_BGR2RGB);
    int panWith = 30;
    int startX = floor(scratchShowImg.cols * point.x / onView.frame.size.width);
    startX = startX - panWith < 0 ? 0 : startX - panWith;
    int startY = floor(scratchShowImg.rows * point.y / onView.frame.size.height);
    
    startY = startY - panWith < 0 ? 0 : startY - panWith;
    for (int x = startX; x < startX + panWith; x ++) {

        for (int y = startY; y < startY + panWith; y ++) {
        
            int b = scratchShowImg.at<Vec3b>(y,x)[0];
            int g = scratchShowImg.at<Vec3b>(y,x)[1];
            int r = scratchShowImg.at<Vec3b>(y,x)[2];
            
            //奖项原图色值获取
            b = showImg.at<Vec3b>(y,x)[0];
            g = showImg.at<Vec3b>(y,x)[1];
            r = showImg.at<Vec3b>(y,x)[2];
            
            //灰色封面色值替换
            scratchShowImg.at<Vec3b>(y,x)[0] = b;
            scratchShowImg.at<Vec3b>(y,x)[1] = g;
            scratchShowImg.at<Vec3b>(y,x)[2] = r;
        }
    }
    cvtColor(scratchShowImg, _scratchCover, cv::COLOR_BGR2RGB, 3);
    UIImage * showImage = MatToUIImage(_scratchCover);
    return showImage;
}

- (void)panAction:(UIPanGestureRecognizer *)sender
{
    switch (sender.state) {
        case UIGestureRecognizerStateChanged:
        {
            CGPoint currentPoint = [sender locationInView:sender.view];
            UIImageView * imageView = (UIImageView *)sender.view;
            imageView.image = [self beginScratchWithPoint:currentPoint onView:sender.view];
        }
            break;
        default:
            break;
    }
}
@end
Copy the code

Fifth, summary and thinking

Simple scratch-off effect is complete, personal summary essay, don’t laugh big God [fist][fist][fist]