Skip to content

Commit ca8d119

Browse files
Add files via upload
1 parent 22a5d43 commit ca8d119

40 files changed

+1124
-0
lines changed

Diff for: app/build.gradle

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
namespace 'com.example.promptimagegenerator'
7+
compileSdk 33
8+
9+
defaultConfig {
10+
applicationId "com.example.promptimagegenerator"
11+
minSdk 24
12+
targetSdk 33
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation 'androidx.appcompat:appcompat:1.6.1'
34+
implementation 'com.google.android.material:material:1.8.0'
35+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
36+
testImplementation 'junit:junit:4.13.2'
37+
implementation 'com.android.volley:volley:1.2.1'
38+
implementation 'com.github.bumptech.glide:glide:4.14.2'
39+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
40+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
41+
}

Diff for: app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.promptimagegenerator;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.example.promptimagegenerator", appContext.getPackageName());
25+
}
26+
}

Diff for: app/src/main/AndroidManifest.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
7+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
8+
9+
<application
10+
android:allowBackup="true"
11+
android:dataExtractionRules="@xml/data_extraction_rules"
12+
android:fullBackupContent="@xml/backup_rules"
13+
android:icon="@mipmap/ic_launcher"
14+
android:label="@string/app_name"
15+
android:supportsRtl="true"
16+
android:requestLegacyExternalStorage="true"
17+
android:theme="@style/Theme.PromptImageGenerator"
18+
tools:targetApi="31">
19+
<activity
20+
android:name=".MainActivity"
21+
android:exported="true">
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
30+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.example.promptimagegenerator;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ImageButton;
9+
import android.widget.ImageView;
10+
import android.widget.Toast;
11+
12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
import androidx.recyclerview.widget.RecyclerView;
15+
16+
import com.bumptech.glide.Glide;
17+
import com.bumptech.glide.load.DataSource;
18+
import com.bumptech.glide.load.engine.GlideException;
19+
import com.bumptech.glide.request.RequestListener;
20+
import com.bumptech.glide.request.target.Target;
21+
22+
import java.io.BufferedOutputStream;
23+
import java.io.File;
24+
import java.io.FileNotFoundException;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.io.OutputStream;
28+
import java.util.ArrayList;
29+
30+
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
31+
Context context;
32+
ArrayList<String> arrayList;
33+
34+
public ImageAdapter(Context context, ArrayList<String> arrayList) {
35+
this.context = context;
36+
this.arrayList = arrayList;
37+
}
38+
39+
@NonNull
40+
@Override
41+
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
42+
View view = LayoutInflater.from(context).inflate(R.layout.image_list_item, parent, false);
43+
return new ViewHolder(view);
44+
}
45+
46+
@Override
47+
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
48+
Glide.with(context).asBitmap().load(arrayList.get(position)).addListener(new RequestListener<Bitmap>() {
49+
@Override
50+
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
51+
return false;
52+
}
53+
54+
@Override
55+
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
56+
holder.imageView.setImageBitmap(resource);
57+
holder.download.setOnClickListener(new View.OnClickListener() {
58+
@Override
59+
public void onClick(View view) {
60+
if (resource != null) {
61+
File file = new File("/storage/emulated/0/Pictures/Generated/image" + position + ".png");
62+
OutputStream outputStream;
63+
try {
64+
outputStream = new BufferedOutputStream(new FileOutputStream(file));
65+
resource.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
66+
outputStream.close();
67+
Toast.makeText(context, "Image Saved at: " + file.getPath(), Toast.LENGTH_SHORT).show();
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
}
73+
});
74+
return true;
75+
}
76+
}).into(holder.imageView);
77+
}
78+
79+
@Override
80+
public int getItemCount() {
81+
return arrayList.size();
82+
}
83+
84+
public static class ViewHolder extends RecyclerView.ViewHolder {
85+
ImageView imageView;
86+
ImageButton download;
87+
public ViewHolder(@NonNull View itemView) {
88+
super(itemView);
89+
imageView = itemView.findViewById(R.id.list_item_image);
90+
download = itemView.findViewById(R.id.list_item_download);
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.example.promptimagegenerator;
2+
3+
import android.content.Context;
4+
import android.widget.Toast;
5+
6+
import com.android.volley.AuthFailureError;
7+
import com.android.volley.DefaultRetryPolicy;
8+
import com.android.volley.Request;
9+
import com.android.volley.RequestQueue;
10+
import com.android.volley.Response;
11+
import com.android.volley.VolleyError;
12+
import com.android.volley.toolbox.JsonObjectRequest;
13+
import com.android.volley.toolbox.Volley;
14+
15+
import org.json.JSONArray;
16+
import org.json.JSONException;
17+
import org.json.JSONObject;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class ImageGenerator {
24+
private final String url = "https://stablediffusionapi.com/api/v3/text2img";
25+
private final Context context;
26+
27+
public ImageGenerator(Context context) {
28+
this.context = context;
29+
}
30+
31+
public void generate(String prompt, int width, int height, int count, OnLoaded onLoaded) {
32+
ArrayList<String> arrayList = new ArrayList<>();
33+
JSONObject js = new JSONObject();
34+
try {
35+
String key = "kHL9nCCUo4p2YZumb8BOx79E7aw5tZeWl9STPfpwB6rOkwgc3dWUqIZ7QDhm";
36+
js.put("key", key);
37+
js.put("prompt", prompt);
38+
js.put("samples", count);
39+
js.put("width", width);
40+
js.put("height", height);
41+
} catch (JSONException e) {
42+
e.printStackTrace();
43+
}
44+
45+
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, js, new Response.Listener<JSONObject>() {
46+
@Override
47+
public void onResponse(JSONObject response) {
48+
if (response != null) {
49+
JSONArray dataArray;
50+
try {
51+
dataArray = response.getJSONArray("output");
52+
for (int i = 0; i < count; i++) {
53+
arrayList.add(dataArray.getString(i));
54+
}
55+
onLoaded.loaded(arrayList);
56+
} catch (JSONException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
}
61+
}, new Response.ErrorListener() {
62+
@Override
63+
public void onErrorResponse(VolleyError error) {
64+
Toast.makeText(context, "There was a error while getting images", Toast.LENGTH_SHORT).show();
65+
}
66+
}) {
67+
@Override
68+
public Map<String, String> getHeaders() throws AuthFailureError {
69+
Map<String, String> params = new HashMap<>();
70+
params.put("Content-type", "application/json");
71+
return params;
72+
}
73+
};
74+
request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
75+
RequestQueue queue = Volley.newRequestQueue(context);
76+
queue.add(request);
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.example.promptimagegenerator;
2+
3+
import androidx.activity.result.ActivityResultCallback;
4+
import androidx.activity.result.ActivityResultLauncher;
5+
import androidx.activity.result.contract.ActivityResultContracts;
6+
import androidx.appcompat.app.AppCompatActivity;
7+
import androidx.core.app.ActivityCompat;
8+
import androidx.recyclerview.widget.RecyclerView;
9+
10+
import android.Manifest;
11+
import android.app.ProgressDialog;
12+
import android.content.pm.PackageManager;
13+
import android.os.Bundle;
14+
import android.view.View;
15+
import android.widget.Button;
16+
import android.widget.SeekBar;
17+
import android.widget.Toast;
18+
19+
import com.google.android.material.textfield.TextInputEditText;
20+
import com.google.android.material.textfield.TextInputLayout;
21+
22+
import java.util.ArrayList;
23+
import java.util.Objects;
24+
25+
public class MainActivity extends AppCompatActivity {
26+
private final ActivityResultLauncher<String> activityResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
27+
@Override
28+
public void onActivityResult(Boolean result) {
29+
if (result) {
30+
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
31+
}
32+
}
33+
});
34+
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.activity_main);
39+
40+
TextInputLayout promptLayout = findViewById(R.id.promptLayout);
41+
TextInputEditText promptET = findViewById(R.id.promptET);
42+
43+
SeekBar width = findViewById(R.id.width);
44+
SeekBar height = findViewById(R.id.height);
45+
SeekBar imageCount = findViewById(R.id.imageCount);
46+
47+
Button generate = findViewById(R.id.generate);
48+
RecyclerView recyclerView = findViewById(R.id.recycler);
49+
50+
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
51+
progressDialog.setMessage("Generating...");
52+
53+
OnLoaded onLoaded = new OnLoaded() {
54+
@Override
55+
public void loaded(ArrayList<String> arrayList) {
56+
progressDialog.dismiss();
57+
ImageAdapter adapter = new ImageAdapter(MainActivity.this, arrayList);
58+
recyclerView.setAdapter(adapter);
59+
}
60+
};
61+
62+
generate.setOnClickListener(new View.OnClickListener() {
63+
@Override
64+
public void onClick(View view) {
65+
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
66+
activityResultLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);
67+
} else {
68+
if (Objects.requireNonNull(promptET.getText()).toString().isEmpty()) {
69+
promptLayout.setError("This Field is Required");
70+
} else {
71+
progressDialog.show();
72+
new ImageGenerator(MainActivity.this).generate(promptET.getText().toString(), width.getProgress(), height.getProgress(), imageCount.getProgress(), onLoaded);
73+
}
74+
}
75+
}
76+
});
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.promptimagegenerator;
2+
3+
import java.util.ArrayList;
4+
5+
public interface OnLoaded {
6+
void loaded(ArrayList<String> arrayList);
7+
}

0 commit comments

Comments
 (0)