|
|
AIWROKÈí¼þµÄBase64¹¤¾ß¼¯
- /**
- * AIWROK Base64 ¹¤¾ß¼¯
- * ¼¯ºÏËùÓÐ Base64 Ïà¹ØÓ÷¨£¬º¬ÍêÕûÈÕÖ¾Êä³öºÍ°¸ÀýÑÝʾ
- */
- // µ¼Èë±ØÒªµÄ Java Àà
- importClass(android.util.Base64);
- importClass(java.io.FileInputStream);
- importClass(java.io.FileOutputStream);
- importClass(java.io.File);
- // ======================== ¹¤¾ßº¯Êý ========================
- /**
- * µ¹¼ÆÊ±£¨Ã룩
- * @param {number} seconds - µ¹¼ÆÊ±ÃëÊý
- */
- function countdown(seconds) {
- for (var i = seconds; i >= 1; i--) {
- printl(" ⏳ " + i + " Ãëºó¿ªÊ¼ÏÂÒ»¸öÑÝʾ...");
- sleep.millisecond(1000);
- }
- printl(" ▶ ¿ªÊ¼£¡");
- }
- /**
- * ·Ö¸ôÏß
- */
- function divider(title) {
- printl("");
- printl("================================================");
- printl(" " + title);
- printl("================================================");
- }
- // ======================== 1. ͼƬ¶ÔÏóת Base64 ========================
- /**
- * AIWROK Image ¶ÔÏóת Base64
- * @param {object} img - AIWROK µÄ Image ¶ÔÏó
- * @returns {string} Base64 ×Ö·û´®
- */
- function imageToBase64(img) {
- try {
- if (!img) {
- printl("[ERROR] imageToBase64: ´«ÈëµÄͼƬ¶ÔÏóΪ¿Õ");
- return null;
- }
- var base64 = img.toBase64();
- printl("[imageToBase64] ת»»³É¹¦£¬´óС: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] imageToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 2. ͼƬÎļþת Base64 ========================
- /**
- * ͼƬÎļþת Base64
- * @param {string} imagePath - ͼƬÎļþ·¾¶
- * @returns {string} Base64 ×Ö·û´®
- */
- function imageFileToBase64(imagePath) {
- try {
- var file = new File(imagePath);
- if (!file.exists()) {
- printl("[ERROR] imageFileToBase64: Îļþ²»´æÔÚ: " + imagePath);
- return null;
- }
- var fis = new FileInputStream(file);
- var bytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, file.length());
- fis.read(bytes);
- fis.close();
- var base64 = Base64.encodeToString(bytes, Base64.NO_WRAP);
- printl("[imageFileToBase64] ת»»³É¹¦£¬Îļþ´óС: " + file.length() + " ×Ö½Ú£¬Base64: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] imageFileToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 3. ×Ö·û´®×ª Base64 ========================
- /**
- * ×Ö·û´®×ª Base64
- * @param {string} str - Òª±àÂëµÄ×Ö·û´®
- * @returns {string} Base64 ×Ö·û´®
- */
- function stringToBase64(str) {
- try {
- var bytes = new java.lang.String(str).getBytes("UTF-8");
- var base64 = Base64.encodeToString(bytes, Base64.NO_WRAP);
- printl("[stringToBase64] ±àÂë³É¹¦: \"" + str + "\" ¡ú " + base64);
- return base64;
- } catch (e) {
- printl("[ERROR] stringToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 4. Base64 ת×Ö·û´® ========================
- /**
- * Base64 ת×Ö·û´®
- * @param {string} base64Str - Base64 ×Ö·û´®
- * @returns {string} ½âÂëºóµÄ×Ö·û´®
- */
- function base64ToString(base64Str) {
- try {
- var bytes = Base64.decode(base64Str, Base64.NO_WRAP);
- var str = new java.lang.String(bytes, "UTF-8");
- printl("[base64ToString] ½âÂë³É¹¦: " + base64Str.substring(0, Math.min(30, base64Str.length())) + "... ¡ú \"" + str + "\"");
- return str;
- } catch (e) {
- printl("[ERROR] base64ToString ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 5. Base64 תͼƬÎļþ ========================
- /**
- * Base64 תͼƬÎļþ
- * @param {string} base64Str - Base64 ×Ö·û´®
- * @param {string} outputPath - Êä³öÎļþ·¾¶
- * @returns {boolean} ÊÇ·ñ³É¹¦
- */
- function base64ToImageFile(base64Str, outputPath) {
- try {
- var bytes = Base64.decode(base64Str, Base64.NO_WRAP);
- var fos = new FileOutputStream(outputPath);
- fos.write(bytes);
- fos.flush();
- fos.close();
- printl("[base64ToImageFile] ±£´æ³É¹¦: " + outputPath + "£¬´óС: " + bytes.length + " ×Ö½Ú");
- return true;
- } catch (e) {
- printl("[ERROR] base64ToImageFile ʧ°Ü: " + e.message);
- return false;
- }
- }
- // ======================== 6. ½ØÍ¼²¢×ª Base64 ========================
- /**
- * È«ÆÁ½ØÍ¼×ª Base64
- * @returns {string} Base64 ×Ö·û´®
- */
- function screenshotToBase64() {
- try {
- var img = screen.screenShotFull();
- if (!img) {
- printl("[ERROR] screenshotToBase64: ½ØÍ¼·µ»Ø¿Õ");
- return null;
- }
- var base64 = img.toBase64();
- img.recycle();
- printl("[screenshotToBase64] ½ØÍ¼×ªBase64³É¹¦£¬´óС: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] screenshotToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 7. ÇøÓò½ØÍ¼²¢×ª Base64 ========================
- /**
- * ÇøÓò½ØÍ¼×ª Base64£¨Ê¹Óà Android Bitmap API ²Ã¼ô£©
- * @param {number} x - ×óÉÏ½Ç X ×ø±ê£¨ÏñËØ£©
- * @param {number} y - ×óÉÏ½Ç Y ×ø±ê£¨ÏñËØ£©
- * @param {number} width - ¿í¶È£¨ÏñËØ£©
- * @param {number} height - ¸ß¶È£¨ÏñËØ£©
- * @returns {string} Base64 ×Ö·û´®
- */
- function regionScreenshotToBase64(x, y, width, height) {
- try {
- // ÏÈÈ«ÆÁ½ØÍ¼
- var img = screen.screenShotFull();
- if (!img) {
- printl("[ERROR] regionScreenshotToBase64: ½ØÍ¼·µ»Ø¿Õ");
- return null;
- }
- // »ñÈ¡ Bitmap ¶ÔÏó
- var bitmap = img.getBitmap();
- if (!bitmap) {
- printl("[ERROR] regionScreenshotToBase64: »ñÈ¡ Bitmap ʧ°Ü");
- img.recycle();
- return null;
- }
- // ʹÓà Android Bitmap API ²Ã¼ô
- var croppedBitmap = android.graphics.Bitmap.createBitmap(bitmap, x, y, width, height);
- img.recycle();
- if (!croppedBitmap) {
- printl("[ERROR] regionScreenshotToBase64: ²Ã¼ôʧ°Ü");
- return null;
- }
- // תΪ Base64
- var baos = new java.io.ByteArrayOutputStream();
- croppedBitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, baos);
- var bytes = baos.toByteArray();
- var base64 = android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP);
-
- croppedBitmap.recycle();
- baos.close();
- printl("[regionScreenshotToBase64] ÇøÓò½ØÍ¼×ªBase64³É¹¦£¬ÇøÓò: (" + x + "," + y + "," + width + "," + height + ")£¬´óС: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] regionScreenshotToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 8. ͨÓÃÎļþת Base64 ========================
- /**
- * ͨÓÃÎļþת Base64
- * @param {string} filePath - Îļþ·¾¶
- * @returns {string} Base64 ×Ö·û´®
- */
- function fileToBase64(filePath) {
- try {
- var file = new File(filePath);
- if (!file.exists()) {
- printl("[ERROR] fileToBase64: Îļþ²»´æÔÚ: " + filePath);
- return null;
- }
- var fis = new FileInputStream(file);
- var bytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, file.length());
- fis.read(bytes);
- fis.close();
- var base64 = Base64.encodeToString(bytes, Base64.NO_WRAP);
- printl("[fileToBase64] ת»»³É¹¦: " + filePath + "£¬´óС: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] fileToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 9. Base64 תͨÓÃÎļþ ========================
- /**
- * Base64 תͨÓÃÎļþ
- * @param {string} base64Str - Base64 ×Ö·û´®
- * @param {string} outputPath - Êä³öÎļþ·¾¶
- * @returns {boolean} ÊÇ·ñ³É¹¦
- */
- function base64ToFile(base64Str, outputPath) {
- try {
- var bytes = Base64.decode(base64Str, Base64.NO_WRAP);
- var fos = new FileOutputStream(outputPath);
- fos.write(bytes);
- fos.flush();
- fos.close();
- printl("[base64ToFile] ±£´æ³É¹¦: " + outputPath + "£¬´óС: " + bytes.length + " ×Ö½Ú");
- return true;
- } catch (e) {
- printl("[ERROR] base64ToFile ʧ°Ü: " + e.message);
- return false;
- }
- }
- // ======================== 10. »ñÈ¡ Base64 ÎļþÀàÐÍ ========================
- /**
- * »ñÈ¡ Base64 ×Ö·û´®µÄÎļþÀàÐÍ
- * @param {string} base64Str - Base64 ×Ö·û´®
- * @returns {string} ÎļþÀàÐÍ
- */
- function getBase64FileType(base64Str) {
- try {
- var bytes = Base64.decode(base64Str.substring(0, 100), Base64.NO_WRAP);
- if (bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47) {
- printl("[getBase64FileType] ¼ì²âµ½ÀàÐÍ: image/png");
- return "image/png";
- }
- if (bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF) {
- printl("[getBase64FileType] ¼ì²âµ½ÀàÐÍ: image/jpeg");
- return "image/jpeg";
- }
- if (bytes[0] == 0x47 && bytes[1] == 0x49 && bytes[2] == 0x46 && bytes[3] == 0x38) {
- printl("[getBase64FileType] ¼ì²âµ½ÀàÐÍ: image/gif");
- return "image/gif";
- }
- printl("[getBase64FileType] δʶ±ðµÄÎļþÀàÐÍ");
- return "unknown";
- } catch (e) {
- printl("[ERROR] getBase64FileType ʧ°Ü: " + e.message);
- return "unknown";
- }
- }
- // ======================== 11. fastCapture ½ØÍ¼×ª Base64 ========================
- /**
- * ¿ìËÙ½ØÍ¼×ª Base64£¨Ê¹Óà screen.screenShotFull£©
- * @returns {string} Base64 ×Ö·û´®
- */
- function fastCaptureToBase64() {
- try {
- var img = screen.screenShotFull();
- if (!img) {
- printl("[ERROR] fastCaptureToBase64: ½ØÍ¼·µ»Ø¿Õ");
- return null;
- }
- var base64 = img.toBase64();
- img.recycle();
- printl("[fastCaptureToBase64] ¿ìËÙ½ØÍ¼×ªBase64³É¹¦£¬´óС: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] fastCaptureToBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ======================== 12. ͼƬ¶ÔÏóת JpgBase64 ========================
- /**
- * AIWROK Image ¶ÔÏóת JPG Base64£¨´øÑ¹Ëõ£©
- * @param {object} img - AIWROK µÄ Image ¶ÔÏó
- * @param {number} quality - JPEG ÖÊÁ¿ 0-100
- * @returns {string} Base64 ×Ö·û´®
- */
- function imageToJpgBase64(img, quality) {
- quality = quality || 80;
- try {
- if (!img) {
- printl("[ERROR] imageToJpgBase64: ´«ÈëµÄͼƬ¶ÔÏóΪ¿Õ");
- return null;
- }
- var base64 = img.toJpgBase64(quality);
- printl("[imageToJpgBase64] JPGѹËõתBase64³É¹¦£¬ÖÊÁ¿: " + quality + "£¬´óС: " + base64.length() + " ×Ö·û");
- return base64;
- } catch (e) {
- printl("[ERROR] imageToJpgBase64 ʧ°Ü: " + e.message);
- return null;
- }
- }
- // ============================================================
- // È«²¿¹¦Äܰ¸ÀýÑÝʾ
- // ============================================================
- function demo() {
- printl("");
- printl("¨X¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨[");
- printl("¨U AIWROK Base64 ¹¤¾ß¼¯ - È«²¿ÑÝʾ ¨U");
- printl("¨U ¹² 12 ¸ö¹¦ÄÜ£¬Öð¸öÑÝʾ ¨U");
- printl("¨^¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨a");
- // ---- ÑÝʾ 1: ×Ö·û´®×ª Base64 ----
- divider("ÑÝʾ 1/12: ×Ö·û´®×ª Base64");
- var testStr = "Hello, AIWROK!";
- printl("[°¸Àý] ½«×Ö·û´® \"" + testStr + "\" ±àÂëΪ Base64");
- var encoded = stringToBase64(testStr);
- printl("[½á¹û] ±àÂëºó: " + encoded);
- countdown(3);
- // ---- ÑÝʾ 2: Base64 ת×Ö·û´® ----
- divider("ÑÝʾ 2/12: Base64 ת×Ö·û´®");
- printl("[°¸Àý] ½« Base64 ½âÂë»ØÔÎÄ");
- var decoded = base64ToString(encoded);
- printl("[½á¹û] ½âÂëºó: " + decoded);
- countdown(3);
- // ---- ÑÝʾ 3: È«ÆÁ½ØÍ¼×ª Base64 ----
- divider("ÑÝʾ 3/12: È«ÆÁ½ØÍ¼×ª Base64");
- printl("[°¸Àý] ½ØÈ¡È«ÆÁ²¢×ªÎª Base64");
- var screenB64 = screenshotToBase64();
- if (screenB64) {
- printl("[½á¹û] ½ØÍ¼Base64´óС: " + screenB64.length() + " ×Ö·û");
- }
- countdown(3);
- // ---- ÑÝʾ 4: ÇøÓò½ØÍ¼×ª Base64 ----
- divider("ÑÝʾ 4/12: ÇøÓò½ØÍ¼×ª Base64");
- printl("[°¸Àý] ½ØÈ¡ÆÁÄ»×óÉÏ½Ç 540x960 ÇøÓò");
- var regionB64 = regionScreenshotToBase64(0, 0, 540, 960);
- if (regionB64) {
- printl("[½á¹û] ÇøÓò½ØÍ¼Base64´óС: " + regionB64.length() + " ×Ö·û");
- }
- countdown(3);
- // ---- ÑÝʾ 5: fastCapture ½ØÍ¼×ª Base64 ----
- divider("ÑÝʾ 5/12: fastCapture ¿ìËÙ½ØÍ¼×ª Base64");
- printl("[°¸Àý] ʹÓà image.fastCapture() ¿ìËÙ½ØÍ¼");
- var fastB64 = fastCaptureToBase64();
- if (fastB64) {
- printl("[½á¹û] ¿ìËÙ½ØÍ¼Base64´óС: " + fastB64.length() + " ×Ö·û");
- }
- countdown(3);
- // ---- ÑÝʾ 6: ͼƬ¶ÔÏóת Base64 ----
- divider("ÑÝʾ 6/12: ͼƬ¶ÔÏóת Base64");
- printl("[°¸Àý] ½ØÍ¼»ñÈ¡ Image ¶ÔÏóºóת Base64");
- var img = screen.screenShotFull();
- if (img) {
- var imgB64 = imageToBase64(img);
- if (imgB64) {
- printl("[½á¹û] ͼƬ¶ÔÏóBase64´óС: " + imgB64.length() + " ×Ö·û");
- }
- img.recycle();
- }
- countdown(3);
- // ---- ÑÝʾ 7: ͼƬ¶ÔÏóת JPG Base64£¨´øÑ¹Ëõ£© ----
- divider("ÑÝʾ 7/12: ͼƬ¶ÔÏóת JPG Base64£¨´øÑ¹Ëõ£©");
- printl("[°¸Àý] ½ØÍ¼ºóÒÔ 50% ÖÊÁ¿Ñ¹ËõΪ JPG Base64");
- var img2 = screen.screenShotFull();
- if (img2) {
- var jpgB64 = imageToJpgBase64(img2, 50);
- if (jpgB64) {
- printl("[½á¹û] JPGѹËõBase64´óС: " + jpgB64.length() + " ×Ö·û");
- printl("[¶Ô±È] ÔPNG´óСԼ " + (screenB64 ? screenB64.length() : 0) + " ×Ö·û£¬JPGѹËõºó " + jpgB64.length() + " ×Ö·û");
- }
- img2.recycle();
- }
- countdown(3);
- // ---- ÑÝʾ 8: ½ØÍ¼±£´æÎªÎļþÔÙת Base64 ----
- divider("ÑÝʾ 8/12: ½ØÍ¼±£´æÎļþ ¡ú Îļþת Base64");
- printl("[°¸Àý] ÏȽØÍ¼±£´æµ½Îļþ£¬ÔÙ´ÓÎļþ¶Áȡת Base64");
- var testImgPath = "/sdcard/base64_test.png";
- var img3 = screen.screenShotFull();
- if (img3) {
- img3.save(testImgPath);
- img3.recycle();
- printl("[²½Öè1] ½ØÍ¼Òѱ£´æ: " + testImgPath);
- var fileB64 = imageFileToBase64(testImgPath);
- if (fileB64) {
- printl("[½á¹û] ÎļþBase64´óС: " + fileB64.length() + " ×Ö·û");
- }
- }
- countdown(3);
- // ---- ÑÝʾ 9: ͨÓÃÎļþת Base64 ----
- divider("ÑÝʾ 9/12: ͨÓÃÎļþת Base64");
- printl("[°¸Àý] ½«ÈÎÒâÎļþתΪ Base64");
- var anyFileB64 = fileToBase64(testImgPath);
- if (anyFileB64) {
- printl("[½á¹û] ͨÓÃÎļþBase64´óС: " + anyFileB64.length() + " ×Ö·û");
- }
- countdown(3);
- // ---- ÑÝʾ 10: Base64 תͼƬÎļþ ----
- divider("ÑÝʾ 10/12: Base64 תͼƬÎļþ");
- printl("[°¸Àý] ½«½ØÍ¼µÄ Base64 »¹ÔΪͼƬÎļþ");
- var outputPath = "/sdcard/base64_restored.png";
- if (screenB64) {
- var ok = base64ToImageFile(screenB64, outputPath);
- if (ok) {
- var restoredFile = new File(outputPath);
- printl("[½á¹û] »¹ÔͼƬ´óС: " + restoredFile.length() + " ×Ö½Ú");
- }
- }
- countdown(3);
- // ---- ÑÝʾ 11: Base64 תͨÓÃÎļþ ----
- divider("ÑÝʾ 11/12: Base64 תͨÓÃÎļþ");
- printl("[°¸Àý] ½« Base64 »¹ÔΪͨÓÃÎļþ");
- var outputPath2 = "/sdcard/base64_restored2.png";
- if (screenB64) {
- var ok2 = base64ToFile(screenB64, outputPath2);
- if (ok2) {
- var restoredFile2 = new File(outputPath2);
- printl("[½á¹û] »¹ÔÎļþ´óС: " + restoredFile2.length() + " ×Ö½Ú");
- }
- }
- countdown(3);
- // ---- ÑÝʾ 12: »ñÈ¡ Base64 ÎļþÀàÐÍ ----
- divider("ÑÝʾ 12/12: »ñÈ¡ Base64 ÎļþÀàÐÍ");
- printl("[°¸Àý] ¼ì²â½ØÍ¼ Base64 µÄÎļþ¸ñʽ");
- if (screenB64) {
- var fileType = getBase64FileType(screenB64);
- printl("[½á¹û] ÎļþÀàÐÍ: " + fileType);
- }
- // ---- ×ܽá ----
- printl("");
- printl("¨X¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨[");
- printl("¨U È«²¿ 12 ¸öÑÝʾÍê³É£¡ ¨U");
- printl("¨d¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨g");
- printl("¨U 1. stringToBase64 ×Ö·û´® ¡ú Base64 ¨U");
- printl("¨U 2. base64ToString Base64 ¡ú ×Ö·û´® ¨U");
- printl("¨U 3. screenshotToBase64 È«ÆÁ½ØÍ¼ ¡ú Base64 ¨U");
- printl("¨U 4. regionScreenshot ÇøÓò½ØÍ¼ ¡ú Base64 ¨U");
- printl("¨U 5. fastCaptureToBase64 ¿ìËÙ½ØÍ¼ ¡ú Base64 ¨U");
- printl("¨U 6. imageToBase64 ͼƬ¶ÔÏó ¡ú Base64 ¨U");
- printl("¨U 7. imageToJpgBase64 ͼƬ ¡ú JPG Base64 ¨U");
- printl("¨U 8. imageFileToBase64 ͼƬÎļþ ¡ú Base64 ¨U");
- printl("¨U 9. fileToBase64 ͨÓÃÎļþ ¡ú Base64 ¨U");
- printl("¨U 10. base64ToImageFile Base64 ¡ú ͼƬÎļþ ¨U");
- printl("¨U 11. base64ToFile Base64 ¡ú ͨÓÃÎļþ ¨U");
- printl("¨U 12. getBase64FileType ¼ì²â Base64 ÀàÐÍ ¨U");
- printl("¨^¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨a");
- }
- // Ö±½ÓÔËÐÐÑÝʾ
- demo();
¸´ÖÆ´úÂë
|
|