Jpeg.java
4.65 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (C) 1998, James R. Weeks and BioElectroMech.
// Visit BioElectroMech at www.obrador.com. Email James@obrador.com.
// This software is based in part on the work of the Independent JPEG Group.
// See license.txt for details about the allowed used of this software.
// See IJGreadme.txt for details about the Independent JPEG Group's license.
package james; // westfeld
import java.awt.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Jpeg {
public static void StandardUsage() {
System.out.println("JpegEncoder for Java(tm) Version 0.9");
System.out.println("");
System.out.println("Program usage: java Jpeg \"InputImage\".\"ext\" Quality [\"OutputFile\"[.jpg]]");
System.out.println("");
System.out.println("Where \"InputImage\" is the name of an existing image in the current directory.");
System.out.println(" (\"InputImage may specify a directory, too.) \"ext\" must be .tif, .gif,");
System.out.println(" or .jpg.");
System.out.println("Quality is an integer (0 to 100) that specifies how similar the compressed");
System.out.println(" image is to \"InputImage.\" 100 is almost exactly like \"InputImage\" and 0 is");
System.out.println(" most dissimilar. In most cases, 70 - 80 gives very good results.");
System.out.println("\"OutputFile\" is an optional argument. If \"OutputFile\" isn't specified, then");
System.out.println(" the input file name is adopted. This program will NOT write over an existing");
System.out.println(" file. If a directory is specified for the input image, then \"OutputFile\"");
System.out.println(" will be written in that directory. The extension \".jpg\" may automatically be");
System.out.println(" added.");
System.out.println("");
System.out.println("Copyright 1998 BioElectroMech and James R. Weeks. Portions copyright IJG and");
System.out.println(" Florian Raemy, LCAV. See license.txt for details.");
System.out.println("Visit BioElectroMech at www.obrador.com. Email James@obrador.com.");
System.exit(0);
}
public static void main(String args[]) {
Image image = null;
FileOutputStream dataOut = null;
File file, outFile;
JpegEncoder jpg;
String string = new String();
int i, Quality = 80;
if (args.length < 2)
StandardUsage();
if (!args[0].endsWith(".jpg") && !args[0].endsWith(".tif") && !args[0].endsWith(".gif"))
StandardUsage();
if (args.length < 3) {
string = args[0].substring(0, args[0].lastIndexOf(".")) + ".jpg";
}
else {
string = args[2];
if (string.endsWith(".tif") || string.endsWith(".gif"))
string = string.substring(0, string.lastIndexOf("."));
if (!string.endsWith(".jpg"))
string = string.concat(".jpg");
}
outFile = new File(string);
i = 1;
while (outFile.exists()) {
outFile = new File(string.substring(0, string.lastIndexOf(".")) + (i++) + ".jpg");
if (i > 100)
System.exit(0);
}
file = new File(args[0]);
if (file.exists()) {
try {
dataOut = new FileOutputStream(outFile);
} catch(IOException e) {}
try {
Quality = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
StandardUsage();
}
image = Toolkit.getDefaultToolkit().getImage(args[0]);
jpg = new JpegEncoder(image, Quality, dataOut);
jpg.Compress();
try {
dataOut.close();
} catch(IOException e) {}
}
else {
System.out.println("I couldn't find " + args[0] + ". Is it in another directory?");
}
System.exit(0);
}
}