-Very Simple Java JPG to ASCII Converter
// Very simple JPG to ASCII converter.
// Written on a saturday night by Heinz Flug .. flu...@online.no
// The code cries out for improvement....
import java.awt.image.BufferedImage;
import java.io.*;
import com.sun.image.codec.jpeg.*;
public class Application1 { // Application1 -- good name, isn't it?
public static void main(String[] args) {
int brightnes = 100;
String fName = "C:/XXX"; // <- Put your filename here..
BufferedImage img = readJPEGImage(fName+".jpg"); // Read the JPG Image
int h = img.getHeight();
int w = img.getWidth();
// Some debug information:
System.out.println(fName+".jpg"+'\t'+"Heigth = "+h+'\t'+"Width = "+w);
// No more dubug info..
String line = null;
writeText(fName+".txt", "", false);
for (int y=1; y<h-2; y=y+2) { // an ASCII char is about
line =""; // 1 pixel wide og 2 pixels high
for (int x=1; x<w; x++) { // (or mabye not..)
int pixel=(getGreyValue(img.getRGB(x,y))+
getGreyValue(img.getRGB(x,y+1)))/2;
pixel = pixel * brightnes / 100;
line = line + getASCIIChar(pixel); //Build line by line..
}
writeText(fName+".txt", line, true); // ..and write it to a txt file
}
}
public static BufferedImage readJPEGImage(String filename) {
BufferedImage image = null;
try { // read the JPG file and return it as an image..
FileInputStream fis = new FileInputStream(filename);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
image = decoder.decodeAsBufferedImage();
fis.close();
}
catch (FileNotFoundException ex) {
System.out.println(ex);
System.exit(0);
}
catch (IOException ex) {
System.out.println(ex);
System.exit(0);
}
return image;
}
public static int getBValue(int rgb) {
return rgb & 0x00FF; // Some sort of magic..
}
public static int getGValue(int rgb) {
return (rgb & 0x00FF00) / 0x100; // another kind of magic..
}
public static int getRValue(int rgb) {
return (rgb & 0x00FF0000) / 0x10000; // pure voodoo..
}
public static int getGreyValue(int rgb) {
return (getRValue(rgb)+ getGValue(rgb) + getBValue(rgb))/3;
}
public static String getASCIIChar(int v) {
String charSets[] = {
"Wmi;. ", // char set #0
"#*+-' ", // char set #1
"@- ", // char set #2
"81: ", // char set #3
"01 ", // char set #4
"81:' " }; // feel free to add whatever you like.
String charSet = charSets[3]; // Choose one of the "character sets".
if (v<0) v = 0; if (v>255) v = 255; // Maybe a good thing to do?
int charPos = v/(1+(255/charSet.length())); // Calculate ASCII char
return charSet.substring(charPos, 1+charPos); // and return it.
}
public static void writeText(String tfile, String txt, boolean append) {
try {
PrintWriter pw = new PrintWriter(
new BufferedWriter(
new FileWriter(tfile,append)));
pw.println(txt); // appends lines to a text file.
pw.close();
}
catch (IOException e) {
System.out.println(e);
System.exit(0);
}
}
} // That's almost all...
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::1:1:::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::11::8:::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::1:1111:8::::::::::::::::::::::::::::::::
:::::::::::::::::::::::11:::::::::::::::11881881:::::::::::::::::::::::::::::::
:::::::::::::::::::::::111:1::::::::::::::181:1811::::::::::::::::::::::::::::::
::::::::::::::::::::::::818111::::::::::::11::18::::::::::::::::::::::::::::::::
:::::::::::::::::::::::1:18811:::::::::::::::11:::::::::::::::::::::::::::::::::
:::::::::::::::::11111:18111811:::::::::::::::1:1:::::::::::::::::::::::::::::::
1:::::::::::::::::11:1111111:::::::::::::::::1::::::::::::::::::1:::::::::::::::
8818:::::::::::::888111188:11:::::::::::::::::::::::::::::::::11::1:::::::::::::
8188:1::::::::::18888881:1::::::::::1:1::::::::::::::::::::::1:::111::::::::::::
881:11:::::::::1: 888811:::::::::::::::1:::11:::1::::::::1111:111:1:::::::::::::
::::::::::::::::::881:: : : ::::::::::::::::::::::::::: : : :: ::::::::::::
1:::::::::::::::::8:: : :::::::::::::: : : : : ::1:::1
81:::::::::::::::1: ::::::: : :::: :::: 111::
111811:::::::::::8: :::: :: ::1:::
811181:1::1:::::::: ::: : ::::1::
1881:888811:::::::: :: ::: :::11:
:881::118818111:::::: :: :::::::::11
18181::::88188881811: ::::::::::::::
118181:::1188888881: :::::::::::::
:118888::::::11881: ::::::::::::1
1:111888888888:: :::::::::::111
:::1:1888881: :::::: :::::::::::::1
::::1888881 : :1811:1 : :::1:1118881111181: : ::::::::11::
::111188881:::881::8188881:::: :::11:: ::18: :::::::::1::
:1188118888 :8: ::1:1:: : : :::881:::::1:11:8
8:111181888:8: ::1:1:: ::: : : : : : :181:11:::: 1
8881:18888811: ::: ::11: ::: ::11:::::::11111::::::
:11881:188818881:: ::::: :: ::: : ::: : 11:::11111:11::::
:1111:1:1881818181888888881::1 : :1111818188111:1::11:::::1::::: 1
1:1888:::88::1888888:181:: ::: :11111818111: :::1:::::1
8 81888::11: :881:: : : :1111:: ::::::::111
8::81881:1: :::::1:::1:8
81111:1:1: :::1:1::1:18
1881::::111 :::::111:1:88
8181:::::18: :: ::: : :::1111111188
8888:::::181: : ::::: ::::::11188118111
11:1::::::11: ::: ::::: :: :::::181188181181
11::::::::811: :111::::111881:::1 :::::11181818818:1
:::::::81181: 8818811188111181: ::::::11118181111 11
1811818811:: :::1:::: :::::111181888181::1:
188888111::: ::: : 1: :::::::11111811888:::11
188181111:: ::: :::::: : :: ::::::::1:11111181118188
:888:1:181:: :111111181111111881811:: :::::::::::111111881811:
111811:8:: : :188818881:111::8:188181811: : :::::::::1118188811:88
: :18881:1:: :181111 1:81181: : :: ::::::::1111188881818
: :1111:11:: 88118:1 :1:188111 : ::::::::1118881818::
:1188881:::::: ::: :88811111::1111811881: :: :: ::::::::::1118188881888
: ::18818111:::: ::: :81881111111181811 : ::::::::::::::111118188888188
111118181:::::: :: :11888888811:: :: ::::::::::::::::111881181818888
: :1811811::::::: : :: : :: ::::::::::::::::11188818881181811
8181881:::: : ::::::::::::::111111118188818::::11
::11111881::: ::::::::::::::::::1111188181188181:::::::
:::11:118111::: ::::::::::::::11111888881111111::::::::
::::::::::1811::::: :: : : ::::::::11111188888888111111181::::::::
:: : : :::8881:::::: :::: ::::::::1111188881888818181118181::::::::
:: : : : 8188811111111111111111818188181181181181111111:81::::::::
: : :::::: ::: 88188888118181818818888888818118118118111:::1:18:::::::::
:: : ::: :::::::::: 18888188888188818888888888881888188111:::::::1:8 ::::::::
:::::::::::::::::::::: :1181118888888888888888888188888111:::(c) Heinz Flug 2001
--
.hF
http://home.online.no/~flug/
Original message headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f996b,7e54e5da9318d064,start X-Google-Attributes: gidf996b,public X-Google-ArrivalTime: 2001-03-26 10:58:02 PST Path: supernews.google.com!sn-xit-03!supernews.com!news-out.usenetserver.com!news-out-sjo.usenetserver.com!news.tele.dk!148.122.208.68!news2.oke.nextra.no!nextra.com!news1.oke.nextra.no.POSTED!not-for-mail From: flu...@online.no (Heinz Flug) Newsgroups: alt.ascii-art Subject: -Very Simple Java JPG to ASCII Converter Message-ID: <3ab...@hard.sjanker.heinzflug.com> X-Newsreader: Forte Agent 1.5/32.452 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 167 Date: Mon, 26 Mar 2001 18:55:25 GMT NNTP-Posting-Host: 130.67.161.145 X-Complaints-To: new...@nextra.no X-Trace: news1.oke.nextra.no 985632956 130.67.161.145 (Mon, 26 Mar 2001 20:55:56 MET DST) NNTP-Posting-Date: Mon, 26 Mar 2001 20:55:56 MET DST Organization: Nextra Public Access Xref: supernews.google.com alt.ascii-art:4907
About this message.
This message was posted publicly to the newsgroup
alt.ascii-art
in 2001 and is mirrored here unchanged as part of the Historic Archives – only email addresses are masked.
The artwork and text belong to their original authors: if you copy a piece, keep the artist's initials or signature intact and credit them where you can.
Are you the author? Contact us to get your posts attributed, connected to your artist profile, or removed.