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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
public final class CameraPanel extends JPanel {
/**
*
*/
private final ReentrantLock lock = new ReentrantLock(false);
/**
*
*/
private final BufferedImage srcImage;
/**
*
*/
private final BufferedImage dispImage;
/**
*
*/
private final DataBufferByte dbb;
/**
*
*/
private final RenderingHints rhSpeed = new RenderingHints(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
/**
*
*/
private final RenderingHints rhColor = new RenderingHints(
RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_SPEED);
/**
* Contructor with panel size, for GrayScale source
* @param w
* @param h
* @param srcBand
*/
public CameraPanel(int w, int h) {
super();
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
dispImage = gc.createCompatibleImage(w, h, Transparency.OPAQUE);
srcImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
dbb = (DataBufferByte) srcImage.getRaster().getDataBuffer();
this.setSize(w, h);
}
/**
* Paint
* @param g
*/
@Override
public void paint(Graphics g) {
Graphics2D gd = (Graphics2D) g;
try {
gd.setRenderingHints(rhSpeed);
lock.lock();
gd.drawImage(dispImage, 0, 0, null);
} catch (Exception ex) {
Logger.getLogger(CameraPanel.class.getName()).log(Level.SEVERE, null, ex);
gd = null;
} finally {
if (lock.isLocked()) {
lock.unlock();
}
}
if (gd != null) {
gd.dispose();
}
}
/**
* Update the underlined image with a ByteBuffer
* @param byteBuffer
*/
public void update(ByteBuffer byteBuffer) {
Graphics2D gd = dispImage.createGraphics();
try {
byteBuffer.rewind();
byteBuffer.get(dbb.getData());
gd.setRenderingHints(rhColor);
lock.lock();
gd.drawImage(srcImage, 0, 0, null);
} catch (Exception ex) {
Logger.getLogger(CameraPanel.class.getName()).log(Level.SEVERE, null, ex);
gd = null;
} finally {
if (lock.isLocked()) {
lock.unlock();
}
}
if (gd != null) {
gd.dispose();
}
}
/**
* Update the underlined image with bytes[]
* @param data
*/
public void update(byte[] data) {
Graphics2D gd = dispImage.createGraphics();
try {
System.arraycopy(data, 0, dbb.getData(), 0, data.length);
gd.setRenderingHints(rhColor);
lock.lock();
gd.drawImage(srcImage, 0, 0, null);
} catch (Exception ex) {
Logger.getLogger(CameraPanel.class.getName()).log(Level.SEVERE, null, ex);
gd = null;
} finally {
if (lock.isLocked()) {
lock.unlock();
}
}
if (gd != null) {
gd.dispose();
}
}
} |
Partager