1
1
package com .wxmimperio .spark
2
2
3
+ import java .awt .image .{BufferedImage , WritableRaster }
4
+ import java .io .{ByteArrayInputStream , File }
5
+
6
+ import javax .imageio .ImageIO
7
+ import org .apache .commons .io .FilenameUtils
3
8
import org .apache .spark .SparkConf
4
9
import org .apache .spark .sql .SparkSession
5
10
6
11
object SimpleDemo {
7
12
8
13
def main (args : Array [String ]): Unit = {
9
- val conf = new SparkConf ().setMaster(" local[*] " )
14
+ val conf = new SparkConf ().setMaster(" local" )
10
15
val spark = SparkSession .builder()
11
16
.config(conf)
12
17
.getOrCreate()
13
18
14
19
val imageDF = spark.read
15
20
.format(" image" )
16
- .load(" E:\\ coding\\ github\\ hadoop-code-snippets\\ spark-best-practice\\ spark-image-process\\ src\\ main\\ resources\\ gorilla_PNG18712.png " )
21
+ .load(" E:\\ coding\\ github\\ hadoop-code-snippets\\ spark-best-practice\\ spark-image-process\\ src\\ main\\ resources\\ pngs \\ " )
17
22
18
23
imageDF.printSchema()
19
24
@@ -26,10 +31,42 @@ object SimpleDemo {
26
31
" image.data"
27
32
)
28
33
29
- row.foreach(row => {
34
+ /* row.foreach(row => {
30
35
val data = row.getAs[Array[Byte]]("data")
31
36
println(data)
37
+ })*/
38
+
39
+ row.repartition(1 ).rdd.map(row => {
40
+ val origin = row.getAs[String ](" origin" )
41
+ println(origin)
42
+ val data = row.getAs[Array [Byte ]](" data" )
43
+ val width = row.getAs[Int ](" width" )
44
+ val height = row.getAs[Int ](" height" )
45
+
46
+
47
+ val image = new BufferedImage (width, height, BufferedImage .TYPE_INT_BGR )
48
+ val raster = image.getData.asInstanceOf [WritableRaster ]
49
+ val pixels = data.map(_.toDouble)
50
+ raster.setPixels(0 , 0 , width, height, pixels)
51
+ image.setData(raster)
52
+
53
+ for (i <- image.getMinX until image.getWidth()) {
54
+ for (j <- image.getMinY until image.getHeight()) {
55
+ println(image.getRGB(i, j))
56
+ }
57
+ }
58
+
59
+ val buffImg = new BufferedImage (width, height, BufferedImage .TYPE_3BYTE_BGR )
60
+ val g = buffImg.createGraphics
61
+ g.drawImage(image, 0 , 0 , null )
62
+ g.dispose()
63
+
64
+ (origin, buffImg)
65
+ }).coalesce(1 ).foreach(buffer => {
66
+ val output = " E:\\ coding\\ github\\ hadoop-code-snippets\\ spark-best-practice\\ spark-image-process\\ src\\ main\\ resources\\ result\\ " + FilenameUtils .getName(buffer._1)
67
+ ImageIO .write(buffer._2, " png" , new File (output))
32
68
})
33
69
70
+ Thread .sleep(100000 )
34
71
}
35
- }
72
+ }
0 commit comments