├── .gitignore ├── LICENSE ├── README.md ├── image.png ├── pom.xml ├── src └── main │ └── java │ └── com │ └── jessetzh │ ├── handler │ ├── BasicParameterHandler.java │ ├── SdResHandler.java │ └── StableDiffusionException.java │ ├── parameters │ ├── BasicParameter.java │ ├── Img2ImgParameter.java │ ├── SamplerEnums.java │ ├── SdParameter.java │ └── Text2ImgParameter.java │ ├── request │ ├── Img2Img.java │ ├── RequestExecutor.java │ └── Text2Img.java │ ├── res │ ├── SdErrorInfo.java │ └── SdResponses.java │ └── test │ └── GeneratorTest.java └── test.jpeg /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | target/ 26 | !.mvn/wrapper/maven-wrapper.jar 27 | !**/src/main/**/target/ 28 | !**/src/test/**/target/ 29 | 30 | ### IntelliJ IDEA ### 31 | .idea/ 32 | *.iws 33 | *.iml 34 | *.ipr 35 | 36 | ### Eclipse ### 37 | .apt_generated 38 | .classpath 39 | .factorypath 40 | .project 41 | .settings 42 | .springBeans 43 | .sts4-cache 44 | 45 | ### NetBeans ### 46 | /nbproject/private/ 47 | /nbbuild/ 48 | /dist/ 49 | /nbdist/ 50 | /.nb-gradle/ 51 | build/ 52 | !**/src/main/**/build/ 53 | !**/src/test/**/build/ 54 | 55 | ### VS Code ### 56 | .vscode/ 57 | 58 | ### Mac OS ### 59 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Jesse 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | SD_JAVA 3 |
4 | Stable-Diffusion-Java 5 |
6 | 一个简单的 stable-diffusion-webui api 调用实现 7 |
8 | An implementation of a simple stable-diffusion-webui API call. 9 |

10 | 11 | ## 💡 简介 12 | 13 | [SD-Java](https://github.com/JesseTzh/stable-diffusion-java) 是一个可以远程调用 stable-diffusion-webui 的 API 方法的工具,现已实现基本的文生图(txt2img)和图生图(img2img)功能接口,并支持身份验证与代理功能。 14 | > 可以直接使用 gradio.live 的 WebUI 地址调用,启动 webui 程序时必须使用 --api 参数 15 | ## ✨ 使用 16 | 17 | 参照 [test](https://github.com/JesseTzh/stable-diffusion-java/tree/main/src/main/java/com/jessetzh/test) 中的范例进行调用 18 | -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JesseTzh/stable-diffusion-java/20f5a076b53f5f0f77d6dd5995d94a3c3b82f429/image.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.jessetzh 8 | stable-diffusion-java 9 | 一个 stable-diffusion-webui api 的 Java 实现类 10 | 0.0.1 11 | 12 | 13 | 8 14 | 8 15 | UTF-8 16 | 17 | 18 | 19 | 20 | 21 | org.apache.httpcomponents 22 | httpclient 23 | 4.5.14 24 | 25 | 26 | 27 | commons-io 28 | commons-io 29 | 2.11.0 30 | 31 | 32 | 33 | com.google.code.gson 34 | gson 35 | 2.8.9 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/handler/BasicParameterHandler.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.handler; 2 | 3 | import com.jessetzh.parameters.BasicParameter; 4 | import org.apache.http.HttpHost; 5 | import org.apache.http.auth.AuthScope; 6 | import org.apache.http.auth.UsernamePasswordCredentials; 7 | import org.apache.http.client.CredentialsProvider; 8 | import org.apache.http.impl.client.BasicCredentialsProvider; 9 | import org.apache.http.impl.client.HttpClientBuilder; 10 | import org.apache.http.impl.conn.DefaultProxyRoutePlanner; 11 | 12 | /** 13 | * Basic parameter validation. 14 | */ 15 | public class BasicParameterHandler { 16 | 17 | public static void check(BasicParameter basicParameter, HttpClientBuilder builder) { 18 | if (basicParameter.isAuthEnable()) { 19 | if (basicParameter.getUser() == null || basicParameter.getPassword() == null) { 20 | throw new StableDiffusionException("The username and password cannot be empty when authentication is enabled!"); 21 | } 22 | // Identity verification information 23 | CredentialsProvider provider = new BasicCredentialsProvider(); 24 | UsernamePasswordCredentials credentials 25 | = new UsernamePasswordCredentials(basicParameter.getUser(), basicParameter.getPassword()); 26 | provider.setCredentials(AuthScope.ANY, credentials); 27 | // Set CredentialsProvider object to HttpClientBuilder 28 | builder.setDefaultCredentialsProvider(provider); 29 | } 30 | if (basicParameter.isProxyEnable()) { 31 | if (basicParameter.getProxyHost() == null || basicParameter.getProxyPort() == 0) { 32 | throw new StableDiffusionException("When using a proxy server, please specify the proxy server address and port."); 33 | } 34 | // Access an API using a proxy server. 35 | builder.setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(basicParameter.getProxyHost(), basicParameter.getProxyPort()))); 36 | } 37 | if (basicParameter.getApiUrl() == null) { 38 | throw new StableDiffusionException("API url can not be empty!"); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/handler/SdResHandler.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.handler; 2 | 3 | import org.apache.http.client.methods.CloseableHttpResponse; 4 | import org.apache.http.util.EntityUtils; 5 | 6 | import java.io.IOException; 7 | 8 | public class SdResHandler { 9 | 10 | public static void process(CloseableHttpResponse response) throws IOException { 11 | if (response.getStatusLine().getStatusCode() != 200) { 12 | if (response.getStatusLine().getStatusCode() == 401) { 13 | throw new StableDiffusionException("Identity verification failed. Please check if your username and password are correct!"); 14 | } else if (response.getStatusLine().getStatusCode() == 500) { 15 | throw new StableDiffusionException(EntityUtils.toString(response.getEntity())); 16 | } else { 17 | throw new StableDiffusionException("Interface call error, please check if the URL is correct."); 18 | } 19 | } else { 20 | if (response.getEntity() == null) { 21 | System.err.println("The result is empty!"); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/handler/StableDiffusionException.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.handler; 2 | 3 | /** 4 | * Abnormal drawing 5 | * 6 | * @author JesseTzh 7 | */ 8 | public class StableDiffusionException extends RuntimeException { 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | public StableDiffusionException(String message) { 13 | super(message); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/parameters/BasicParameter.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.parameters; 2 | 3 | public class BasicParameter { 4 | 5 | private String apiUrl; 6 | 7 | private boolean proxyEnable; 8 | 9 | private String proxyHost; 10 | 11 | private int proxyPort; 12 | 13 | private boolean authEnable; 14 | 15 | private String user; 16 | 17 | private String password; 18 | 19 | public String getApiUrl() { 20 | return apiUrl; 21 | } 22 | 23 | public void setApiUrl(String apiUrl) { 24 | this.apiUrl = apiUrl; 25 | } 26 | 27 | public boolean isProxyEnable() { 28 | return proxyEnable; 29 | } 30 | 31 | public void setProxyEnable(boolean proxyEnable) { 32 | this.proxyEnable = proxyEnable; 33 | } 34 | 35 | public String getProxyHost() { 36 | return proxyHost; 37 | } 38 | 39 | public void setProxyHost(String proxyHost) { 40 | this.proxyHost = proxyHost; 41 | } 42 | 43 | public int getProxyPort() { 44 | return proxyPort; 45 | } 46 | 47 | public void setProxyPort(int proxyPort) { 48 | this.proxyPort = proxyPort; 49 | } 50 | 51 | public boolean isAuthEnable() { 52 | return authEnable; 53 | } 54 | 55 | public void setAuthEnable(boolean authEnable) { 56 | this.authEnable = authEnable; 57 | } 58 | 59 | public String getUser() { 60 | return user; 61 | } 62 | 63 | public void setUser(String user) { 64 | this.user = user; 65 | } 66 | 67 | public String getPassword() { 68 | return password; 69 | } 70 | 71 | public void setPassword(String password) { 72 | this.password = password; 73 | } 74 | 75 | public BasicParameter(String url) { 76 | this.proxyEnable = false; 77 | this.authEnable = false; 78 | if (url != null) { 79 | if ( url.endsWith("/")) { 80 | url = url.substring(0, url.length() - 1); 81 | } 82 | } 83 | this.apiUrl = url; 84 | } 85 | 86 | public BasicParameter() { 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/parameters/Img2ImgParameter.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.parameters; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.math.BigDecimal; 6 | 7 | public class Img2ImgParameter extends SdParameter { 8 | 9 | String[] init_images; 10 | 11 | @SerializedName("denoising_strength") 12 | private BigDecimal denoisingStrength; 13 | 14 | public BigDecimal getDenoisingStrength() { 15 | return denoisingStrength; 16 | } 17 | 18 | public void setDenoisingStrength(BigDecimal denoisingStrength) { 19 | this.denoisingStrength = denoisingStrength; 20 | } 21 | 22 | public String[] getInit_images() { 23 | return init_images; 24 | } 25 | 26 | public void setInit_images(String[] init_images) { 27 | this.init_images = init_images; 28 | } 29 | 30 | public Img2ImgParameter() { 31 | super(); 32 | } 33 | 34 | public Img2ImgParameter(String url) { 35 | super(url); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/parameters/SamplerEnums.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.parameters; 2 | 3 | public enum SamplerEnums { 4 | 5 | Euler_a(1, "Euler_a"), 6 | Euler(2, "Euler"), 7 | LMS(3, "LMS"), 8 | Heun(4, "Heun"), 9 | DPM2(5, "DPM2"), 10 | DPM2_a(6, "DPM2 a"), 11 | DPM_2S_a(7, "DPM++ 2S a"), 12 | DPM_2M(8, "DPM++ 2M"), 13 | DPM_SDE(9, "DPM++ SDE"), 14 | DPM_fast(10, "DPM fast"), 15 | DPM_adaptive(11, "DPM adaptive"), 16 | LMS_Karras(12, "LMS Karras"), 17 | DPM2_Karras(13, "DPM2 Karras"), 18 | DPM2_a_Karras(14, "DPM2 a Karras"), 19 | DPM_2S_a_Karras(15, "DPM++ 2S a Karras"), 20 | DPM_2M_Karras(16, "DPM++ 2M Karras"), 21 | DPM_SDE_Karras(17, "DPM++ SDE Karras"), 22 | DDIM(18, "DDIM"), 23 | PLMS(19, "PLMS"), 24 | UniPC(20, "UniPC"); 25 | 26 | private final Integer code; 27 | 28 | private final String info; 29 | 30 | public Integer getCode() { 31 | return code; 32 | } 33 | 34 | public String getInfo() { 35 | return info; 36 | } 37 | 38 | SamplerEnums(int i, String samplerName) { 39 | this.code = i; 40 | this.info = samplerName; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/parameters/SdParameter.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.parameters; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.math.BigDecimal; 6 | 7 | public class SdParameter { 8 | 9 | private transient BasicParameter basicParameter; 10 | 11 | private String prompt; 12 | 13 | @SerializedName("negative_prompt") 14 | private String negativePrompt; 15 | 16 | private int steps; 17 | 18 | @SerializedName("resize_mode") 19 | private int resizeMode; 20 | 21 | @SerializedName("cfg_scale") 22 | private int cfgScale; 23 | 24 | private int seed; 25 | 26 | private int width; 27 | 28 | private int height; 29 | 30 | @SerializedName("sampler_name") 31 | private String samplerName; 32 | 33 | public int getResizeMode() { 34 | return resizeMode; 35 | } 36 | 37 | public void setResizeMode(int resizeMode) { 38 | this.resizeMode = resizeMode; 39 | } 40 | 41 | public int getCfgScale() { 42 | return cfgScale; 43 | } 44 | 45 | public void setCfgScale(int cfgScale) { 46 | this.cfgScale = cfgScale; 47 | } 48 | 49 | public int getSeed() { 50 | return seed; 51 | } 52 | 53 | public void setSeed(int seed) { 54 | this.seed = seed; 55 | } 56 | 57 | public int getWidth() { 58 | return width; 59 | } 60 | 61 | public void setWidth(int width) { 62 | this.width = width; 63 | } 64 | 65 | public int getHeight() { 66 | return height; 67 | } 68 | 69 | public void setHeight(int height) { 70 | this.height = height; 71 | } 72 | 73 | public String getSamplerName() { 74 | return samplerName; 75 | } 76 | 77 | public void setSamplerName(String samplerName) { 78 | this.samplerName = samplerName; 79 | } 80 | 81 | public int getSteps() { 82 | return steps; 83 | } 84 | 85 | public void setSteps(int steps) { 86 | this.steps = steps; 87 | } 88 | 89 | public String getNegativePrompt() { 90 | return negativePrompt; 91 | } 92 | 93 | public void setNegativePrompt(String negativePrompt) { 94 | this.negativePrompt = negativePrompt; 95 | } 96 | 97 | public BasicParameter getBasicParameter() { 98 | return basicParameter; 99 | } 100 | 101 | public void setBasicParameter(BasicParameter basicParameter) { 102 | this.basicParameter = basicParameter; 103 | } 104 | 105 | public String getPrompt() { 106 | return prompt; 107 | } 108 | 109 | public void setPrompt(String prompt) { 110 | this.prompt = prompt; 111 | } 112 | 113 | public SdParameter() { 114 | 115 | } 116 | 117 | public SdParameter(String url) { 118 | this.basicParameter = new BasicParameter(url); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/parameters/Text2ImgParameter.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.parameters; 2 | 3 | public class Text2ImgParameter extends SdParameter{ 4 | 5 | public Text2ImgParameter() { 6 | super(); 7 | } 8 | 9 | public Text2ImgParameter(String url) { 10 | super(url); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/request/Img2Img.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.request; 2 | 3 | import com.google.gson.GsonBuilder; 4 | import com.jessetzh.handler.BasicParameterHandler; 5 | import com.jessetzh.parameters.Img2ImgParameter; 6 | import com.jessetzh.res.SdResponses; 7 | import org.apache.http.client.methods.HttpPost; 8 | import org.apache.http.entity.StringEntity; 9 | import org.apache.http.impl.client.HttpClientBuilder; 10 | import org.apache.http.impl.client.HttpClients; 11 | 12 | import java.io.IOException; 13 | 14 | public class Img2Img { 15 | 16 | private static final String API_PATH = "/sdapi/v1/img2img"; 17 | 18 | public static SdResponses generate(Img2ImgParameter parameter) throws IOException { 19 | HttpClientBuilder builder = HttpClients.custom(); 20 | BasicParameterHandler.check(parameter.getBasicParameter(), builder); 21 | parameter.getBasicParameter().setApiUrl(parameter.getBasicParameter().getApiUrl() + API_PATH); 22 | HttpPost httpPost = new HttpPost(parameter.getBasicParameter().getApiUrl()); 23 | StringEntity entity = new StringEntity(new GsonBuilder().create().toJson(parameter)); 24 | return RequestExecutor.execute(builder, entity, httpPost); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/request/RequestExecutor.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.request; 2 | 3 | import com.google.gson.Gson; 4 | import com.jessetzh.handler.SdResHandler; 5 | import com.jessetzh.handler.StableDiffusionException; 6 | import com.jessetzh.res.SdResponses; 7 | import org.apache.http.client.methods.CloseableHttpResponse; 8 | import org.apache.http.client.methods.HttpPost; 9 | import org.apache.http.entity.StringEntity; 10 | import org.apache.http.impl.client.CloseableHttpClient; 11 | import org.apache.http.impl.client.HttpClientBuilder; 12 | 13 | import java.io.IOException; 14 | import java.io.InputStreamReader; 15 | 16 | public class RequestExecutor { 17 | 18 | public static SdResponses execute(HttpClientBuilder builder, StringEntity entity, HttpPost httpPost) throws IOException { 19 | CloseableHttpClient httpClient = null; 20 | CloseableHttpResponse response = null; 21 | SdResponses responses = new SdResponses(); 22 | httpPost.setEntity(entity); 23 | httpPost.setHeader("Accept", "application/json"); 24 | httpPost.setHeader("Content-type", "application/json"); 25 | try { 26 | httpClient = builder.build(); 27 | long startTime = System.currentTimeMillis(); 28 | response = httpClient.execute(httpPost); 29 | System.out.println("Request duration: " + (System.currentTimeMillis() - startTime) + "ms"); 30 | // Process the request result. 31 | SdResHandler.process(response); 32 | Gson gson = new Gson(); 33 | responses = gson.fromJson(new InputStreamReader(response.getEntity().getContent()), SdResponses.class); 34 | } catch (StableDiffusionException e) { 35 | System.err.println("Stable-Diffusion request error!"); 36 | throw new StableDiffusionException(e.getMessage()); 37 | } catch (IOException e) { 38 | throw new RuntimeException(e); 39 | } finally { 40 | if (response != null) { 41 | response.close(); 42 | } 43 | if (httpClient != null) { 44 | httpClient.close(); 45 | } 46 | } 47 | return responses; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/request/Text2Img.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.request; 2 | 3 | import com.google.gson.GsonBuilder; 4 | import com.jessetzh.handler.BasicParameterHandler; 5 | import com.jessetzh.parameters.Text2ImgParameter; 6 | import com.jessetzh.res.SdResponses; 7 | import org.apache.http.client.methods.HttpPost; 8 | import org.apache.http.entity.StringEntity; 9 | import org.apache.http.impl.client.HttpClientBuilder; 10 | import org.apache.http.impl.client.HttpClients; 11 | 12 | import java.io.IOException; 13 | 14 | public class Text2Img { 15 | 16 | private static final String API_PATH = "/sdapi/v1/img2img"; 17 | 18 | public static SdResponses generate(Text2ImgParameter parameter) throws IOException { 19 | HttpClientBuilder builder = HttpClients.custom(); 20 | BasicParameterHandler.check(parameter.getBasicParameter(), builder); 21 | parameter.getBasicParameter().setApiUrl(parameter.getBasicParameter().getApiUrl() + API_PATH); 22 | HttpPost httpPost = new HttpPost(parameter.getBasicParameter().getApiUrl()); 23 | StringEntity entity = new StringEntity(new GsonBuilder().create().toJson(parameter)); 24 | return RequestExecutor.execute(builder, entity, httpPost); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/res/SdErrorInfo.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.res; 2 | 3 | public class SdErrorInfo { 4 | String detail; 5 | 6 | public String getDetail() { 7 | return detail; 8 | } 9 | 10 | public void setDetail(String detail) { 11 | this.detail = detail; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/res/SdResponses.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.res; 2 | 3 | 4 | import com.jessetzh.parameters.SdParameter; 5 | 6 | public class SdResponses { 7 | 8 | private String[] images; 9 | 10 | private SdParameter parameters; 11 | 12 | private String info; 13 | 14 | public String[] getImages() { 15 | return images; 16 | } 17 | 18 | public void setImages(String[] images) { 19 | this.images = images; 20 | } 21 | 22 | public SdParameter getParameters() { 23 | return parameters; 24 | } 25 | 26 | public void setParameters(SdParameter parameters) { 27 | this.parameters = parameters; 28 | } 29 | 30 | public String getInfo() { 31 | return info; 32 | } 33 | 34 | public void setInfo(String info) { 35 | this.info = info; 36 | } 37 | 38 | public SdResponses() { 39 | } 40 | 41 | public SdResponses(String[] images, SdParameter parameters, String info) { 42 | this.images = images; 43 | this.parameters = parameters; 44 | this.info = info; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/jessetzh/test/GeneratorTest.java: -------------------------------------------------------------------------------- 1 | package com.jessetzh.test; 2 | 3 | 4 | import com.jessetzh.parameters.Img2ImgParameter; 5 | import com.jessetzh.parameters.SamplerEnums; 6 | import com.jessetzh.parameters.Text2ImgParameter; 7 | import com.jessetzh.request.Img2Img; 8 | import com.jessetzh.request.Text2Img; 9 | import com.jessetzh.res.SdResponses; 10 | 11 | import javax.imageio.ImageIO; 12 | import java.awt.image.BufferedImage; 13 | import java.io.ByteArrayInputStream; 14 | import java.io.File; 15 | import java.io.IOException; 16 | import java.math.BigDecimal; 17 | import java.nio.file.Files; 18 | import java.nio.file.Path; 19 | import java.nio.file.Paths; 20 | import java.util.Base64; 21 | 22 | /** 23 | * 24 | */ 25 | 26 | public class GeneratorTest { 27 | 28 | //支持直接使用 gradio.live WebUI 地址调用 29 | static final String URL = "https://xxxxxxxxxxxxx.gradio.live/"; 30 | 31 | public static void main(String[] args) throws IOException { 32 | new GeneratorTest().img2imgTest(); 33 | } 34 | 35 | private void text2ImgText() throws IOException { 36 | Text2ImgParameter parameter = new Text2ImgParameter(URL); 37 | //如需要代理则解开下列代码注释 38 | // parameter.getBasicParameter().setProxyEnable(true); 39 | // parameter.getBasicParameter().setProxyHost("127.0.0.1"); 40 | // parameter.getBasicParameter().setProxyPort(7890); 41 | parameter.setPrompt("One Golden Retriever"); 42 | SdResponses res = Text2Img.generate(parameter); 43 | for (String image : res.getImages()) { 44 | BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(image))); 45 | File output = new File("image.png"); 46 | ImageIO.write(bufferedImage, "png", output); 47 | } 48 | } 49 | 50 | private void img2imgTest() throws IOException { 51 | Img2ImgParameter parameter = new Img2ImgParameter(URL); 52 | //重绘幅度 53 | parameter.setDenoisingStrength(new BigDecimal("0.55")); 54 | parameter.setSeed(-1); 55 | parameter.setSteps(30); 56 | parameter.setCfgScale(7); 57 | parameter.setWidth(512); 58 | parameter.setHeight(512); 59 | parameter.setSamplerName(SamplerEnums.DPM_2M_Karras.getInfo()); 60 | Path path = Paths.get("test.jpeg"); 61 | byte[] bytes = Files.readAllBytes(path); 62 | Base64.Encoder encoder = Base64.getEncoder(); 63 | String base64String = encoder.encodeToString(bytes); 64 | parameter.setInit_images(new String[]{base64String}); 65 | parameter.setPrompt("dog"); 66 | parameter.setNegativePrompt("cat"); 67 | SdResponses res = Img2Img.generate(parameter); 68 | for (String image : res.getImages()) { 69 | BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(image))); 70 | File output = new File("image.png"); 71 | ImageIO.write(bufferedImage, "png", output); 72 | System.out.println(res.getInfo()); 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /test.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JesseTzh/stable-diffusion-java/20f5a076b53f5f0f77d6dd5995d94a3c3b82f429/test.jpeg --------------------------------------------------------------------------------