11.7 图像增强与动漫化一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package org.example; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; public class AuthService { private static final Logger logger = Logger.getLogger(AuthService. class .getName()); public static String getAuth(String ak, String sk) { String authHost = "https://aip.baidubce.com/oauth/2.0/token?" ; String getAccessTokenUrl = authHost + "grant_type=client_credentials" + "&client_id=" + ak + "&client_secret=" + sk; try { URL realUrl = new URL(getAccessTokenUrl); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setRequestMethod( "GET" ); int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300 ) { try (BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()))) { StringBuilder result = new StringBuilder(); String line; while ((line = in.readLine()) != null ) { result.append(line); } JSONObject jsonObject = new JSONObject(result.toString()); return jsonObject.getString( "access_token" ); } } else { logger.log(Level.SEVERE, "请求失败,状态码: " + responseCode); } } catch (IOException e) { logger.log(Level.SEVERE, "获取token失败!" , e); } return null ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package org.example; /** * Base64 工具类 */ public class Base64Util { private static final char last2byte = ( char ) Integer.parseInt( "00000011" , 2 ); private static final char last4byte = ( char ) Integer.parseInt( "00001111" , 2 ); private static final char last6byte = ( char ) Integer.parseInt( "00111111" , 2 ); private static final char lead6byte = ( char ) Integer.parseInt( "11111100" , 2 ); private static final char lead4byte = ( char ) Integer.parseInt( "11110000" , 2 ); private static final char lead2byte = ( char ) Integer.parseInt( "11000000" , 2 ); private static final char [] encodeTable = new char []{ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' }; public Base64Util() { } public static String encode( byte [] from) { StringBuilder to = new StringBuilder(( int ) (( double ) from.length * 1 .34D) + 3 ); int num = 0 ; char currentByte = 0 ; int i; for (i = 0 ; i < from.length; ++i) { for (num %= 8 ; num < 8 ; num += 6 ) { switch (num) { case 0 : currentByte = ( char ) (from[i] & lead6byte); currentByte = ( char ) (currentByte >>> 2 ); case 1 : case 3 : case 5 : default : break ; case 2 : currentByte = ( char ) (from[i] & last6byte); break ; case 4 : currentByte = ( char ) (from[i] & last4byte); currentByte = ( char ) (currentByte << 2 ); if (i + 1 < from.length) { currentByte = ( char ) (currentByte | (from[i + 1 ] & lead2byte) >>> 6 ); } break ; case 6 : currentByte = ( char ) (from[i] & last2byte); currentByte = ( char ) (currentByte << 4 ); if (i + 1 < from.length) { currentByte = ( char ) (currentByte | (from[i + 1 ] & lead4byte) >>> 4 ); } } to.append(encodeTable[currentByte]); } } if (to.length() % 4 != 0 ) { for (i = 4 - to.length() % 4 ; i > 0 ; --i) { to.append( "=" ); } } return to.toString(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package org.example; import java.io.*; /** * 文件读取工具类 */ public class FileUtil { /** * 读取文件内容,作为字符串返回 */ public static String readFileAsString(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException(filePath); } if (file.length() > 1024 * 1024 * 1024 ) { throw new IOException( "File is too large" ); } StringBuilder sb = new StringBuilder(( int ) (file.length())); // 创建字节输入流 FileInputStream fis = new FileInputStream(filePath); // 创建一个长度为10240的Buffer byte [] bbuf = new byte [ 10240 ]; // 用于保存实际读取的字节数 int hasRead = 0 ; while ( (hasRead = fis.read(bbuf)) > 0 ) { sb.append( new String(bbuf, 0 , hasRead)); } fis.close(); return sb.toString(); } /** * 根据文件路径读取byte[] 数组 */ public static byte [] readFileByBytes(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException(filePath); } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(( int ) file.length()); BufferedInputStream in = null ; try { in = new BufferedInputStream( new FileInputStream(file)); short bufSize = 1024 ; byte [] buffer = new byte [bufSize]; int len1; while (- 1 != (len1 = in.read(buffer, 0 , bufSize))) { bos.write(buffer, 0 , len1); } byte [] var7 = bos.toByteArray(); return var7; } finally { try { if (in != null ) { in.close(); } } catch (IOException var14) { var14.printStackTrace(); } bos.close(); } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通