Mercurial > cgi-bin > hgweb.cgi > JpegWasher
annotate src/name/blackcap/exifwasher/Test2.kt @ 3:19c381c536ec
Code to make it a proper Mac GUI app. Untested!
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 08 Apr 2020 20:29:12 -0700 |
parents | efd9fe2d70d7 |
children |
rev | line source |
---|---|
2
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
1 /* |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
2 * A basic test of the library: try to use it to print out the EXIF |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
3 * data. |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
4 */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
5 package name.blackcap.exifwasher |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
6 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
7 import java.io.File |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 import java.io.FileInputStream |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 import java.io.FileOutputStream |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 import name.blackcap.exifwasher.exiv2.* |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
11 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
12 /* entry point */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
13 fun main(args: Array<String>) { |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
14 /* must have a file name */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
15 if (args.size != 1) { |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
16 System.err.println("expecting a file name") |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 System.exit(1) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 /* copy the file; we don't want to scribble on the original */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 val (name, ext) = splitext(args[0]) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 val newName = "${name}_washed${ext}" |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 FileInputStream(args[0]).use { source -> |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 FileOutputStream(newName).use { target -> |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
25 source.copyTo(target) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
28 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
29 /* load the whitelist and image data */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 val white = Whitelist.parse(PROPERTIES.getProperty("whitelist")) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
31 val image = Image(newName) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
33 /* do the washing */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
34 val meta = image.metadata |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
35 val keys = meta.keys |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
36 val keysin = keys.size |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
37 var deleted = 0 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 keys.forEach { |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
39 if (!white.contains(it)) { |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
40 meta.erase(it) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 deleted += 1 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
42 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
43 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
44 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
45 /* save and summarize */ |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 image.store() |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
47 println("${keysin} in - ${deleted} deleted = ${keysin - deleted} out") |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 fun splitext(s: String): Pair<String, String> { |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 val pos = s.lastIndexOf('.') |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 if (pos == -1) { |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 return Pair(s, "") |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 } |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 return Pair(s.substring(0, pos), s.substring(pos)) |
efd9fe2d70d7
Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 } |