java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法詳解
本文實(shí)例講述了java圖片滑動(dòng)驗(yàn)證(登錄驗(yàn)證)原理與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這是我簡(jiǎn)單做出的效果圖,處理300X150px的校驗(yàn)圖,并把圖片發(fā)到前端,用時(shí)50毫秒左右,速度還是非??斓摹?/p>
原理:
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有顏色,如下圖,我要摳圖的輪廓是這樣的:
左邊和下邊有有半圓,這個(gè)根據(jù)圓的公式就可以了,代碼示例:
static int targetLength=55;//小圖長(zhǎng) 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 h1 = 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 - h1,2); double d2 = Math.pow(j-2,2) + Math.pow(i - h1,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ì)有所幫助。
- 5分鐘教你使用java搞定網(wǎng)站登錄驗(yàn)證碼
- Java實(shí)現(xiàn)注冊(cè)登錄與郵箱發(fā)送賬號(hào)驗(yàn)證激活功能
- Java?自定義注解在登錄驗(yàn)證的應(yīng)用示例
- Java登錄功能實(shí)現(xiàn)token生成與驗(yàn)證
- 教你用Java驗(yàn)證服務(wù)器登錄系統(tǒng)
- Java Web制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析
- JavaWeb簡(jiǎn)單用戶登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)
- Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能
- java實(shí)現(xiàn)登錄驗(yàn)證碼
- Java基于Session登錄驗(yàn)證的實(shí)現(xiàn)示例
相關(guān)文章
Java可變個(gè)數(shù)形參的方法實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Java可變個(gè)數(shù)形參的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02

以實(shí)例簡(jiǎn)介Java中線程池的工作特點(diǎn)

java數(shù)據(jù)結(jié)構(gòu)之java實(shí)現(xiàn)棧

SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)

使用IDEA如何打包發(fā)布SpringBoot并部署到云服務(wù)器

關(guān)于報(bào)錯(cuò)IDEA Terminated with exit code

springcloud如何獲取網(wǎng)關(guān)封裝的頭部信息