Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelshkjony committed Aug 14, 2022
0 parents commit a6e8e56
Show file tree
Hide file tree
Showing 29 changed files with 319 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Output directory
out
13 changes: 13 additions & 0 deletions .idea/img2-ascii.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/libraries/deps.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/libraries/dev-deps.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added .rush/build.hive
Empty file.
Empty file added .rush/build.lock
Empty file.
Binary file added .rush/data.hive
Binary file not shown.
Empty file added .rush/data.lock
Empty file.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Img2Ascii

An App Inventor 2 extension created using Rush.
#ImageToAscii
Binary file added assets/aix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/completed-kodular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/completed-mit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/create-kodular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/create-mit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/failed-kodular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/failed-mit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/image-to-ascii.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/progress-kodular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/progress-mit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions deps/.placeholder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory stores your extension's dependencies.
36 changes: 36 additions & 0 deletions rush.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# For a detailed info on this file and supported fields, check out the below link:
# https://github.com/ShreyashSaitwal/rush-cli/wiki/Metadata-File

---
name: ImgToAscii
description: Developed by Jewel Shikder Jony<br><br><a href='https://fb.com/jewelshkjony/' target='_blank'>Facebook</a> | <a href='https://m.me/jewelshkjony/' target='_blank'>Messenger</a><br><a href='https://t.me/jewelshkjony/' target='_blank'>Telegram</a> | <a href='https://wa.me/8801775668913' target='_blank'>WhatsApp</a><br><br><a href='https://github.com/jewelshkjony?tab=repositories' target='_blank'>More Extensions</a>
homepage: https://github.com/jewelshkjony/ImgToAscii
license: https://github.com/jewelshkjony/ImgToAscii/blob/main/LICENSE

version:
number: 1
name: '1.0'

authors:
- Jewel Shikder Jony

build:
release:
# Optimizes the extension on every release build.
optimize: true

# If enabled, you will be able to use Java 8 language features in your extension.
desugar:
enable: true
desugar_deps: false

assets:
# Extension icon. This can be a URL or a local image in 'assets' folder.
icon: icon.png
# Extension assets.
# other:
# - my_awesome_asset.anything

# Extension dependencies (JAR).
# deps:
# - my_awesome_library.jar
10 changes: 10 additions & 0 deletions src/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jewel.imgtoascii">

<uses-permission android:name="android.permission.INTERNET" />

<application>
</application>

</manifest>
123 changes: 123 additions & 0 deletions src/com/jewel/imgtoascii/Img2Ascii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.jewel.imgtoascii;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

import static java.lang.Math.round;

public class Img2Ascii {

private String[] chars = {"@", "#", "+", "\\", ";", ":", ",", ".", "`", " "};
private Bitmap rgbImage;
private Boolean color = false;
private int quality = 3;
private int qualityColor = 6;
private Spannable response;
private Listener listener;

public Img2Ascii(){

}

public Img2Ascii bitmap(Bitmap rgbImage){
this.rgbImage = rgbImage;
return this;
}

public Img2Ascii quality(int quality){
this.quality = quality;
return this;
}

public Img2Ascii color(Boolean color){
this.color = color;
return this;
}

public void convert(Listener listener) {
this.listener = listener;
new InstaApi().execute();
}

@SuppressLint("StaticFieldLeak")
private class InstaApi extends AsyncTask<String, Integer, Void> {

private InstaApi(){

}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Void doInBackground(String... arg0) {
if(color) {
quality = quality + qualityColor;
if (quality > 5 + qualityColor || quality < 1 + qualityColor)
quality = 3 + qualityColor;
}else{
if (quality > 5 || quality < 1)
quality = 3;
}
String tx;
SpannableStringBuilder span = new SpannableStringBuilder();
int width = rgbImage.getWidth();
int height = rgbImage.getHeight();
int i = 0;
for (int y = 0; y < height; y = y + quality) {
for (int x = 0; x < width; x = x + quality) {
int pixel = rgbImage.getPixel(x, y);
int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);
if(color) {
tx = "#";
span.append(tx);
span.setSpan(new ForegroundColorSpan(Color.rgb(red, green, blue)), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else {
int brightness = red + green + blue;
brightness = round(brightness / (765 / (chars.length - 1)));
tx = chars[brightness];
span.append(tx);
}
i++;
}
tx = "\n";
span.append(tx);
publishProgress(y, height);
i++;
if(isCancelled()) break;
}
response = span;

return null;
}

protected void onProgressUpdate(Integer... progress) {
int current = progress[0];
int total = progress[1];
int percentage = 100 * current / total;
listener.onProgress(percentage);
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
listener.onResponse(response);
}

}

public interface Listener {
void onProgress(int percentage);
void onResponse(Spannable response);
}

}
77 changes: 77 additions & 0 deletions src/com/jewel/imgtoascii/ImgToAscii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.jewel.imgtoascii;

import java.io.IOException;
import java.net.URL;

import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.widget.TextView;

public class ImgToAscii extends AndroidNonvisibleComponent {

public ImgToAscii(ComponentContainer container) {
super(container.$form());
}

@SimpleFunction(description = "Set quality as int from 1-5. Set very small text size to view the full ascii image.")
public void Create(String path, int quality, boolean isColor, AndroidViewComponent textView) {
TextView view = (TextView) textView.getView();
if (path.startsWith("http://") || path.startsWith("https://")) {
new Thread(() -> {
try {
Bitmap bitmap = BitmapFactory.decodeStream(new URL(path).openStream());
form.$context().runOnUiThread(() -> {
ConvertView(view, quality, isColor, bitmap);
});
} catch (IOException e) {
Failed(e.getMessage());
}
}).start();
} else {
Bitmap bitmap = BitmapFactory.decodeFile(path);
ConvertView(view, quality, isColor, bitmap);
}
}

private void ConvertView(TextView textView, int quality, boolean isColor, Bitmap bitmap) {
new Img2Ascii()
.bitmap(bitmap)
.quality(quality)
.color(isColor)
.convert(new Img2Ascii.Listener() {
@Override
public void onProgress(int percentage) {
Progress(percentage);
}

@Override
public void onResponse(Spannable text) {
textView.setText(text);
Completed();
}
});
}

@SimpleEvent(description = "")
public void Progress(int percentage) {
EventDispatcher.dispatchEvent(this, "Progress", percentage);
}

@SimpleEvent(description = "")
public void Completed() {
EventDispatcher.dispatchEvent(this, "Completed");
}

@SimpleEvent(description = "")
public void Failed(String message) {
EventDispatcher.dispatchEvent(this, "Failed", message);
}
}
15 changes: 15 additions & 0 deletions src/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Add any ProGuard configurations specific to this
# extension here.

-keep public class com.jewel.imgtoascii.ImgToAscii {
public *;
}
-keeppackagenames gnu.kawa**, gnu.expr**

-optimizationpasses 5
-allowaccessmodification
-mergeinterfacesaggressively

-repackageclasses 'com/jewel/imgtoascii/repack'
-flattenpackagehierarchy
-dontpreverify

0 comments on commit a6e8e56

Please sign in to comment.