-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageGeneratorTest.java
58 lines (51 loc) · 1.87 KB
/
ImageGeneratorTest.java
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
import edu.vadym.vorobel.ImageGenerator;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
/**
* Created by Vadym on 10.01.2016.
*/
public class ImageGeneratorTest {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
ImageGenerator imageGenerator = new ImageGenerator(new Size(1024, 768), 10);
Mat generatedImage = imageGenerator.generate();
displayImage(generatedImage, "Generated image");
}
public static void displayImage(Mat matImg, String title){
Image img = Mat2BufferedImage(matImg);
ImageIcon icon=new ImageIcon(img);
JFrame frame = new JFrame();
frame.setSize(img.getWidth(null)+50, img.getHeight(null)+50);
JScrollPane jsp = new JScrollPane(new JLabel(icon));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(jsp);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(title);
}
/*
Ïåðåòâîðþº Mat çîáðàæåííÿ â BufferedImage
äëÿ òîãî, ùîá éîãî ìîæíà áóëî ïîêàçàòè â JFrame
*/
public static BufferedImage Mat2BufferedImage(Mat m){
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0, 0, b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
}