บทความนี้แชร์รหัสการตรวจสอบรูปแบบการตรวจสอบ QQ รูปแบบการเลียนแบบรูปแบบการตรวจสอบภาพสำหรับทุกคน เนื้อหาเฉพาะมีดังนี้
คุณสมบัติรวมถึง: ขนาดภาพที่กำหนดเองและความยาวอักขระสีพื้นหลังแบบสุ่มและสีอักขระมุมออฟเซ็ตอักขระแบบสุ่มขอบอักขระเรียบเส้นสัญญาณรบกวนเสียงรบกวนพื้นหลัง
ตอนแรกฉันต้องการทำให้ตัวละครบิดตัว แต่ฉันไม่รู้ว่าทำไมครูจึงเขียนข้อความแล้วรายงานข้อผิดพลาด มาทำกันเลย ฉันหวังว่าอาจารย์สามารถช่วยแก้ไขได้
ควรสังเกตว่าเหตุผลที่มีรหัสการตรวจสอบ QQ เล็กน้อยเป็นเพราะฟอนต์แอลจีเรียนี้ หากระบบไม่มีระบบคุณต้องติดตั้งด้วยตัวเอง คุณสามารถดาวน์โหลดชื่อตัวอักษรบน Baidu และทิ้งโฟลเดอร์ System Fonts
ภาพการทำซ้ำ:
แพ็คเกจ hh.com.util; นำเข้า java.io.ioException; นำเข้า javax.servlet.servletexception; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า Javax.servlet.http.httpsession; Authimage คลาสสาธารณะขยาย javax.servlet.http.httpservlet ใช้ javax.servlet.servlet {คงที่สุดท้าย Long SerialVersionUid = 1L; บริการโมฆะสาธารณะ (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setheader ("pragma", "no-cache"); Response.Setheader ("แคชควบคุม", "ไม่มีแคช"); Response.setDateHeader ("หมดอายุ", 0); Response.SetContentType ("Image/JPEG"); // สร้างสตริงแบบสุ่มสตริง verifyCode = VerifyCodeUtils.GenerateverifyCode (4); // บันทึกเซสชันเซสชัน httpsession เซสชัน = request.getSession (จริง); Session.setAttribute ("Rand", VerifyCode.ToLowerCase ()); // สร้างภาพ int w = 200, h = 80; VerifyCodeUtils.OutputImage (W, H, Response.getOutputStream (), VerifyCode); - คลาส VerifyCodeUtils
แพ็คเกจ hh.com.util; นำเข้า java.awt.Color; นำเข้า java.awt.font; นำเข้า java.awt.graphics; นำเข้า java.awt.graphics2d นำเข้า Java.awt.LineargradientPaint; นำเข้า Java.awt.paint; java.awt.image.bufferedimage; นำเข้า java.io.file; นำเข้า java.io.fileoutputstream; นำเข้า java.io.ioexception; นำเข้า java.io.OutputStream; นำเข้า Java.util.Arrays; นำเข้า Java.util.Random; นำเข้า Javax.imageio.imageio; Public Class VerifyCodeUtils {// ใช้แบบอักษรอัลจีเรียหากไม่ได้อยู่ในระบบตัวอักษรจะแสดงเป็นตัวพิมพ์ใหญ่เท่านั้นและลบอักขระที่สับสนได้อย่างง่ายดายของ 1,0, i, o สตริงสุดท้ายคงที่สาธารณะ Verify_codes = "23456789abcdefghjklmnpqrstuwv สุ่มแบบคงที่ส่วนตัวแบบสุ่ม = ใหม่สุ่ม (); / *** ใช้แหล่งข้อมูลอักขระเริ่มต้นของระบบเพื่อสร้างรหัสการตรวจสอบ* @param Verifysize ความยาวรหัสการตรวจสอบ* @return*/ สตริงคงที่สาธารณะ GenerateverifyCode (int VerifySize, Verify_codes); } / *** ใช้แหล่งที่มาเพื่อสร้างรหัสการตรวจสอบ* @param ตรวจสอบความยาวรหัสการตรวจสอบ* @param แหล่งที่มาการตรวจสอบรหัสอักขระแหล่งที่มา* @return* / สตริงคงที่สาธารณะ generateverifyCode (int Verifysize, แหล่งที่มาของสตริง) } int codeslen = sources.length (); สุ่ม rand = ใหม่สุ่ม (system.currenttimeMillis ()); StringBuilder VerifyCode = new StringBuilder (VerifySize); สำหรับ (int i = 0; i <verifySize; i ++) {VerifyCode.Append (sources.charat (rand.nextint (codeslen-1))); } return verifyCode.toString (); } / ** * สร้างไฟล์รหัสการตรวจสอบแบบสุ่มและส่งคืนค่ารหัสการตรวจสอบ * @param w * @param h * @param outputfile * @param verifysize * @return * @throws ioexception * / สตริงคงที่ zoextezation outputImage (w, h, outputfile, verifyCode); ส่งคืน VerifyCode; } / ** * เอาต์พุตสตรีมการตรวจสอบภาพแบบสุ่มและส่งคืนค่ารหัสการตรวจสอบ * @param w * @param h * @param os * @param revifysize * @return * @throws ioexception * / สตริงคงที่ provat outputImage (W, H, OS, VerifyCode); ส่งคืน VerifyCode; } / ** * สร้างไฟล์ภาพการตรวจสอบที่ระบุไฟล์ภาพ * @param w * @param h * @param outputfile * @param รหัส * @throws ioexception * / โมฆะคงที่สาธารณะ } ไฟล์ dir = outputFile.getParentFile (); if (! dir.exists ()) {dir.mkdir (); } ลอง {outputFile.CreatenewFile (); fileOutputStream fos = new fileOutputStream (outputFile); outputImage (w, h, fos, รหัส); fos.close (); } catch (ioexception e) {โยน e; }} / ** * เอาท์พุทระบุรหัสการตรวจสอบภาพสตรีมภาพ * @param w * @param h * @param os * @param รหัส * @throws ioexception * / โมฆะคงที่สาธารณะคงที่ BufferedImage Image = New BufferedImage (W, H, BufferedImage.type_int_rgb); สุ่มแรนด์ = ใหม่สุ่ม (); graphics2d g2 = image.creategraphics (); g2.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on); สี [] สี = สีใหม่ [5]; สี [] colorspaces = สีใหม่ [] {color.white, color.cyan, color.gray, color.light_gray, color.magenta, color.orange, color.pink, color.yellow}; float [] fractions = new float [colours.length]; สำหรับ (int i = 0; i <colours.length; i ++) {colors [i] = colorspaces [rand.nextint (colorspaces.length)]; เศษส่วน [i] = rand.nextfloat (); } array.sort (เศษส่วน); g2.setColor (color.gray); // ตั้งค่าสีชายแดน G2.FillRect (0, 0, W, H); Color C = GetRandColor (200, 250); g2.setColor (c); // ตั้งค่าสีพื้นหลัง g2.fillrect (0, 2, w, h-4); // วาดเส้นสัญญาณรบกวนแบบสุ่ม = ใหม่สุ่ม (); g2.setColor (getRandColor (160, 200)); // ตั้งค่าสีของบรรทัดสำหรับ (int i = 0; i <20; i ++) {int x = random.nextint (w - 1); int y = random.nextint (h - 1); int xl = random.nextint (6) + 1; int yl = random.nextint (12) + 1; G2.Drawline (x, y, x + xl + 40, y + yl + 20); } // เพิ่มเสียงรบกวนจากการหาววววววววววววววววววววววววววว สำหรับ (int i = 0; i <พื้นที่; i ++) {int x = random.nextint (w); int y = random.nextint (h); int rgb = getRandomIntColor (); image.setrgb (x, y, rgb); } Shear (G2, W, H, C); // บิดเบือนรูปภาพ G2.SetColor (GetRandColor (100, 160)); int fontsize = H-4; FONT FONT = FONT ใหม่ ("Algerian", Font.italic, FontSize); G2.SetFont (แบบอักษร); char [] chars = code.tochararray (); สำหรับ (int i = 0; i <verifysize; i ++) {affineTransform affine = new affineTransform (); Affine.settorotation (math.pi / 4 * rand.nextdouble () * (rand.nextboolean ()? 1: -1), (w / verifysize) * i + fontsize / 2, h / 2); G2.SetTransform (Affine); G2.DrawChars (Chars, I, 1, ((W -10)/Verifysize) * i + 5, H/2 + Fontsize/2 - 10); } g2.dispose (); Imageio.write (รูปภาพ, "jpg", OS); } สีคงที่ส่วนตัว getRandColor (int fc, int bc) {ถ้า (fc> 255) fc = 255; ถ้า (BC> 255) BC = 255; int r = fc + random.nextint (bc - fc); int g = fc + random.nextint (bc - fc); int b = fc + random.nextint (bc - fc); คืนสีใหม่ (R, G, B); } ส่วนตัวคงที่ int getRandomintColor () {int [] rgb = getRandomRgb (); สี int = 0; สำหรับ (int c: rgb) {color = color << 8; สี = สี | C; } กลับสี; } ส่วนตัวคงที่ int [] getRandomRgb () {int [] rgb = new int [3]; สำหรับ (int i = 0; i <3; i ++) {rgb [i] = random.nextint (255); } return rgb; } แรงเฉือนแบบคงที่ส่วนตัว (กราฟิก g, int w1, int h1, สีสี) {shearx (g, w1, h1, สี); Sheary (G, W1, H1, สี); } โมฆะคงที่ส่วนตัว Shearx (กราฟิก g, int w1, int h1, สีสี) {ช่วงเวลา int = random.nextint (2); Boolean Bordergap = true; เฟรม int = 1; int phase = random.nextint (2); สำหรับ (int i = 0; i <h1; i ++) {double d = (double) (ระยะเวลา >> 1) * math.sin ((สองครั้ง) i / (สองเท่า) ระยะเวลา+(6.2831853071795862d * (สองเท่า) เฟส) / (สองเท่า) เฟรม); G.Copyarea (0, I, W1, 1, (int) d, 0); if (bordergap) {g.setColor (สี); G.Drawline ((int) d, i, 0, i); G.Drawline ((int) d + w1, i, w1, i); }}} โมฆะคงที่ส่วนตัว sheary (กราฟิก g, int w1, int h1, สีสี) {int period = random.nextint (40) + 10; // 50; Boolean Bordergap = true; เฟรม int = 20; ระยะ int = 7; สำหรับ (int i = 0; i <w1; i ++) {double d = (double) (ระยะเวลา >> 1) * math.sin ((สองครั้ง) i / (สองเท่า) ระยะเวลา+(6.2831853071795862d * (สองเท่า) เฟส) / (สองเท่า) เฟรม); G.Copyarea (i, 0, 1, H1, 0, (int) d); if (bordergap) {g.setColor (สี); G.Drawline (i, (int) d, i, 0); G.Drawline (i, (int) d + h1, i, h1); }}} โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น ioexception {ไฟล์ dir = ไฟล์ใหม่ ("f:/verifier"); int w = 200, h = 80; สำหรับ (int i = 0; i <50; i ++) {string verifyCode = generateverifyCode (4); ไฟล์ไฟล์ = ไฟล์ใหม่ (dir, verifyCode + ".jpg"); outputImage (W, H, ไฟล์, VerifyCode); - การกำหนดค่า web.xml:
<servlet> <servlet-name> authimage </servlet-name> <servlet-class> hh.com.util.authimage </servlet-lass> </servlet> <servlet-mapping> <servlet-name> Authimage </servlet-name>
ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ ฉันหวังว่ามันจะเป็นแรงบันดาลใจให้คุณเรียนรู้รหัสการตรวจสอบภาพ Java