java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法詳解

本文實(shí)例講述了java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

在大余等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)營銷網(wǎng)站建設(shè),大余網(wǎng)站建設(shè)費(fèi)用合理。

這是我簡單做出的效果圖,處理300X150px的校驗(yàn)圖,并把圖片發(fā)到前端,用時(shí)50毫秒左右,速度還是非??斓摹?/p>

 java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法詳解

原理:

1.利用java從大圖中隨機(jī)摳出一張小圖,并在大圖上給摳出小圖的位置加陰影,然后把這兩張圖片返回給前端;

2.前端獲取圖片,用戶滑動(dòng)小圖到陰影的位置,獲取小圖滑動(dòng)的距離,返回給java后臺(tái)進(jìn)行校驗(yàn);

3.校驗(yàn)通過,返回校驗(yàn)通過編號(hào);

4.前端調(diào)登錄接口,把賬號(hào)、密碼、和校驗(yàn)編號(hào)傳到Java后臺(tái)進(jìn)行登錄。

實(shí)現(xiàn):

1.計(jì)算需要的小圖輪廓,用二維數(shù)組來表示,二維數(shù)組有兩張值,0和1,其中0表示沒有顏色,1有顏色,如下圖,我要摳圖的輪廓是這樣的:

java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法詳解

左邊和下邊有有半圓,這個(gè)根據(jù)圓的公式就可以了,代碼示例:

static int targetLength=55;//小圖長
static int targetWidth=45;//小圖寬
static int circleR=6;//半徑
static int r1=3;//距離點(diǎn)
/**
 * 
* @Createdate: 2019年1月24日上午10:52:42
* @Title: getBlockData
* @Description: 生成小圖輪廓
* @author mzl
* @return int[][]
* @throws
 */
private static int[][] getBlockData() {
    int[][] data = new int[targetLength][targetWidth];
    double x2 = targetLength-circleR;
    //隨機(jī)生成圓的位置
    double h2 = circleR + Math.random() * (targetWidth-3*circleR-r1);
    double po = circleR*circleR;
    double xbegin = targetLength-circleR-r1;
    double ybegin = targetWidth-circleR-r1;
    for (int i = 0; i < targetLength; i++) {
        for (int j = 0; j < targetWidth; j++) {
            double d3 = Math.pow(i - x2,2) + Math.pow(j - h2,2);
            double d2 = Math.pow(j-2,2) + Math.pow(i - h2,2);
            if ((j <= ybegin && d2 <= po)||(i >= xbegin && d3 >= po)) {
                data[i][j] = 0;
            } else {
                data[i][j] = 1;
            }
        }
    }
    return data;
}

2.根據(jù)計(jì)算處理的小圖輪廓,在大圖上摳圖

/**
 * 
* @Createdate: 2019年1月24日上午10:51:30
* @Title: cutByTemplate
* @Description: 生成小圖片、給大圖片添加陰影
* @author mzl
* @param oriImage
* @param targetImage
* @param templateImage
* @param x
* @param y void
* @throws
 */
private static void cutByTemplate(BufferedImage oriImage,BufferedImage targetImage, int[][] templateImage, int x,int y){
    for (int i = 0; i < targetLength; i++) {
        for (int j = 0; j < targetWidth; j++) {
            int rgb = templateImage[i][j];
            // 原圖中對(duì)應(yīng)位置變色處理
            int rgb_ori = oriImage.getRGB(x + i, y + j);
            if (rgb == 1) {
      //摳圖上復(fù)制對(duì)應(yīng)顏色值
                targetImage.setRGB(i, j, rgb_ori);
      //原圖對(duì)應(yīng)位置顏色變化
                oriImage.setRGB(x + i, y + j, rgb_ori & 0x363636 );
            }else{
                //這里把背景設(shè)為透明
                targetImage.setRGB(i, j, rgb_ori & 0x00ffffff);
            }
        }
    }
}

3.把大圖小圖轉(zhuǎn)base64碼,方便返回給前端

/**
 * 
* @Createdate: 2019年1月24日上午11:49:42
* @Title: createImage
* @Description: 獲取大圖,小圖Base64碼
* @author mzl
* @param url
* @return Map<String,String>
* @throws
 */
public static Map<String,String> createImage(String url,int L,int W,Map<String,String> resultMap){
     try {
         BufferedImage bufferedImage = ImageIO.read(new FileInputStream(url));
         BufferedImage target= new BufferedImage(targetLength, targetWidth, BufferedImage.TYPE_4BYTE_ABGR);
         cutByTemplate(bufferedImage,target,getBlockData(),L,W);
         resultMap.put("b", getImageBASE64(bufferedImage));//大圖
         resultMap.put("s", getImageBASE64(target));//小圖
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        return resultMap;
    }
}
/**
* 
* @Createdate: 2019年1月24日上午11:14:19
* @Title: getImageStr
* @Description: 圖片轉(zhuǎn)BASE64
* @author mzl
* @param image
* @return
* @throws IOException String
* @throws
*/
public static String getImageBASE64(BufferedImage image) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image,"png",out);
        byte[] b = out.toByteArray();//轉(zhuǎn)成byte數(shù)組
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(b);//生成base64編碼 
}

到此圖片驗(yàn)證關(guān)鍵代碼完畢。

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

新聞標(biāo)題:java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法詳解
轉(zhuǎn)載來于:http://bm7419.com/article4/ipdsoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈企業(yè)建站、面包屑導(dǎo)航手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)