Guest User

Untitled

a guest
Jan 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. //This method will capture the image from the interface and save it under the unique employee ID
  2. public String captureImage(int picId){
  3.  
  4. FrameGrabbingControl ControlFG = (FrameGrabbingControl)
  5.  
  6. broadcast.getControl("javax.media.control.FrameGrabbingControl");
  7.  
  8. Buffer buffer = ControlFG.grabFrame();
  9.  
  10. BufferToImage image = new BufferToImage((VideoFormat)buffer.getFormat());
  11.  
  12. img = image.createImage(buffer);
  13.  
  14. path="c:\employee"+picId+".jpg";
  15.  
  16. saveJPG(img,path);//method will save the image
  17.  
  18. return path;
  19.  
  20. }
  21.  
  22. public void saveJPG(Image img, String s){***//method will save the image***
  23.  
  24. System.out.println(s);
  25.  
  26. BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null),
  27.  
  28. BufferedImage.TYPE_INT_RGB);
  29.  
  30. Graphics2D g2 = bi.createGraphics();
  31.  
  32. g2.drawImage(img,null,null);
  33.  
  34. FileOutputStream out = null;
  35. try{
  36.  
  37. out = new FileOutputStream(s);
  38.  
  39. }
  40. catch (java.io.FileNotFoundException io){
  41.  
  42. System.out.println("File Not Found");
  43. }
  44.  
  45. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  46.  
  47. JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
  48.  
  49. param.setQuality(0.5f,false);
  50.  
  51. encoder.setJPEGEncodeParam(param);
  52.  
  53. try
  54. {
  55. encoder.encode(bi);
  56. out.close();
  57. }
  58.  
  59. catch (java.io.IOException io)
  60. {
  61. System.out.println("IOException");
  62. }
  63. }
  64.  
  65. import javax.swing.ImageIcon;
  66. import java.awt.image.BufferedImage;
  67. import java.awt.Image;
  68. import java.awt.Color;
  69. import java.awt.Graphics2D;
  70. import java.io.File;
  71. import javax.imageio.ImageIO;
  72. import java.awt.RenderingHints;
  73.  
  74. public class ImgUtils {
  75.  
  76. public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
  77. BufferedImage bi = null;
  78. try {
  79. ImageIcon ii = new ImageIcon(filename);//path to image
  80. bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  81. Graphics2D g2d = (Graphics2D) bi.createGraphics();
  82. g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
  83. g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. return null;
  87. }
  88. return bi;
  89. }
  90.  
  91. }
  92.  
  93. final BufferedImage img=new ImgUtils().scaleImage(200,200,"c:/test.jpg");
  94. //create label with image as background
  95. JLabel label=new JLabel(new ImageIcon((Image)img));
  96.  
  97. import java.awt.BorderLayout;
  98. import java.awt.Graphics2D;
  99. import java.awt.Image;
  100. import java.awt.RenderingHints;
  101. import java.awt.image.BufferedImage;
  102. import javax.swing.ImageIcon;
  103. import javax.swing.JFrame;
  104. import javax.swing.JLabel;
  105. import javax.swing.SwingUtilities;
  106.  
  107. public class JavaApplication117 {
  108.  
  109. //change this to your own
  110. static String filename="c:/test.jpg";
  111.  
  112. public static void main(String[] args) {
  113. SwingUtilities.invokeLater(new Runnable() {
  114.  
  115. @Override
  116. public void run() {
  117. new JavaApplication117().createAndShowUI();
  118. }
  119. });
  120. }
  121.  
  122. private void createAndShowUI() {
  123. JFrame frame = new JFrame("Test");
  124. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  125.  
  126. initComponents(frame);
  127.  
  128. frame.setResizable(false);
  129. frame.pack();
  130. frame.setVisible(true);
  131. }
  132.  
  133. private void initComponents(JFrame frame) {
  134. final BufferedImage img = new ImgUtils().scaleImage(200, 200, filename);
  135. //create label with image as background
  136. JLabel label = new JLabel(new ImageIcon((Image) img));
  137.  
  138. frame.getContentPane().add(label, BorderLayout.CENTER);
  139. }
  140. }
  141.  
  142. class ImgUtils {
  143.  
  144. public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
  145. BufferedImage bi = null;
  146. try {
  147. ImageIcon ii = new ImageIcon(filename);//path to image
  148. bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  149. Graphics2D g2d = (Graphics2D) bi.createGraphics();
  150. g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
  151. g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
  152. } catch (Exception e) {
  153. e.printStackTrace();
  154. return null;
  155. }
  156. return bi;
  157. }
  158. }
Add Comment
Please, Sign In to add comment