Don't like ads? PRO users don't see any ads ;-)
Guest

verimagen

By: a guest on May 7th, 2012  |  syntax: Java  |  size: 6.95 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import javax.imageio.*;
  3. import javax.imageio.stream.*;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.sql.*;
  7. import java.io.*;
  8. import oracle.jdbc.driver.*;
  9. import oracle.ord.im.*;
  10. import java.awt.event.*;
  11. import javax.swing.border.Border;
  12.  
  13. public class HelloDBImage implements ActionListener{
  14.         Principal p = new Principal(null);
  15.         OracleConnection con = null;
  16.         JLabel label = null;
  17.         public String textFieldString = "Entre el Id de la Imagen:";
  18.         public String mimeType,formato,ancho,alto,sig;
  19.         public void actionPerformed(ActionEvent e)
  20.         {
  21.                 String prefix = "Usted Escribio \"";
  22.  
  23.                 if (e.getActionCommand().equals(textFieldString))
  24.                 {
  25.                         JTextField source = (JTextField)e.getSource();
  26.                
  27.                         ImageIcon imgIcon = createImageIcon(source.getText());
  28.                         if (null != imgIcon)
  29.                         {
  30.                                 // Create image Label
  31.                                 label.setText(null);
  32.                                 label.setIcon(imgIcon);
  33.                         }
  34.                         else
  35.                         {
  36.                                 label.setIcon(null);
  37.                                 label.setText("Imagen no Encontra! ");
  38.                         }
  39.                         source.setText("");
  40.                         label.repaint();
  41.                         //actionLabel.setText(prefix + source.getText() + "\"");
  42.                 }
  43.         }
  44.  
  45.         public ImageIcon createImageIcon(String key)
  46.         {
  47.                 Image image = null;
  48.                 ImageIcon imgIcon = null;
  49.  
  50.                 try
  51.                 {
  52.                         // Connect to the database if necessary
  53.                         if (null == con) con = connect();
  54.                         // Create Input Stream from DB image.
  55.                         InputStream is = new BufferedInputStream(getDBInputStream(con, key));
  56.                         if (null != is){
  57.                                 // Create Image from Image Input Stream
  58.                                 ImageInputStream iis = ImageIO.createImageInputStream(is);
  59.                                 image = ImageIO.read(iis);
  60.                                 // Create Image Icon from ImageInputStream
  61.                                 if (null != image) imgIcon = new ImageIcon(image);
  62.                         }
  63.                 } catch (Exception e)
  64.                 {
  65.                         System.out.println("exception raised " + e);
  66.                         e.printStackTrace();
  67.                 }
  68.                 return imgIcon;
  69.         }
  70.  
  71.         public Component createPanelAndContents(String key)
  72.         {
  73.                 // Create the image label
  74.                 ImageIcon imgIcon = createImageIcon(key);
  75.                 if (null != imgIcon)
  76.                 {// Create image Label
  77.                         label = new JLabel(imgIcon);
  78.                        
  79.                 }else{
  80.                         label = new JLabel("Imagen no Encontrada! ");
  81.                 }
  82.                 // Create layout
  83.                 GridBagLayout gridBag = new GridBagLayout();
  84.                 // Create panel to draw in
  85.                 JPanel pane = new JPanel();
  86.                 pane.setLayout(gridBag);
  87.                 GridBagConstraints c = new GridBagConstraints();
  88.                 JScrollPane scrollPane = new JScrollPane(label,
  89.                                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  90.                                 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  91.                 c.fill = GridBagConstraints.BOTH;
  92.                 c.weightx = 1.0;
  93.                 c.weighty = 1.0;
  94.                 gridBag.setConstraints(scrollPane, c);
  95.                 c.gridwidth = GridBagConstraints.REMAINDER;
  96.                 c.fill = GridBagConstraints.HORIZONTAL;
  97.                 scrollPane.setMinimumSize(new Dimension(300, 300));
  98.                 pane.add(scrollPane, c);
  99.                 // Add a text field and label to get anoter image
  100.                 JTextField textField = new JTextField(10);
  101.                 textField.setActionCommand(textFieldString);
  102.                 textField.addActionListener(this);
  103.                 // Add fields for input
  104.                 JLabel promptLabel = new JLabel("Por Favor Entre un ID de Imagen :");
  105.                 pane.add(promptLabel);
  106.                 pane.add(textField, c);
  107.                
  108.                 System.out.println("add jpanel: "+key);
  109.                 return pane;
  110.         }
  111.  
  112.         public static void main(String[] args){
  113.                 String key = "152";
  114.                 try{
  115.                         UIManager.setLookAndFeel(
  116.                                         UIManager.getCrossPlatformLookAndFeelClassName());
  117.                 } catch (Exception e) { }
  118.                 //Create the top-level container and add contents to it.
  119.                 JFrame frame = new JFrame("Aplicacion Visualizacion de Imagenes.");
  120.                 HelloDBImage app = new HelloDBImage();
  121.                 if ((args.length > 0) && (args[0] != null)) key = args[0];
  122.                 System.out.print("length= " + args.length + "/" );
  123.                 if (args.length > 0) System.out.println(args[0]);
  124.                 Component contents = app.createPanelAndContents(key);
  125.                 frame.getContentPane().add(contents, BorderLayout.CENTER);
  126.                 //Finish setting up the frame, and show it.
  127.                 frame.addWindowListener(new WindowAdapter() {
  128.                         public void windowClosing(WindowEvent e) {
  129.                                 System.exit(0);
  130.                         }
  131.                 });
  132.                
  133.                ImageIcon icon = app.createImageIcon("152");
  134.                JLabel label= new JLabel("A default label", icon, JLabel.LEFT);
  135.                Border border = BorderFactory.createLineBorder(Color.BLACK);
  136.                label.setBorder(border);
  137.                frame.add(label);
  138.                frame.pack();
  139.                frame.setVisible(true);
  140.         }
  141.  
  142.         public InputStream getDBInputStream(OracleConnection con, String key)
  143.         {
  144.                 InputStream is = null;
  145.  
  146.                 try
  147.                 {
  148.                         int index = 0;
  149.                         Statement s = con.createStatement();
  150.                         oracle.jdbc.OracleResultSet rs =
  151.                                 (oracle.jdbc.OracleResultSet)s.executeQuery("SELECT * FROM imagenes_objeto " +
  152.                                                                                     "WHERE idimagenes="+ key );
  153.  
  154.                         if (rs.next()) // Just get first image if more than 1.
  155.                         {
  156.                                 index = rs.getInt(2);
  157.                                   System.out.println("select "+index);
  158.                                 OrdImage imgObj = (OrdImage) rs.getCustomDatum(1, OrdImage.getFactory());
  159.                                 OrdImageSignature imgObjSign = (OrdImageSignature)rs.getORAData("firma_imagen", OrdImageSignature.getORADataFactory());
  160.                                
  161.                                 System.out.println("Signature : " + imgObjSign.toString());
  162.                                
  163.                     mimeType= imgObj.getMimeType();
  164.                     formato= imgObj.getCompressionFormat();
  165.                     ancho = ""+imgObj.getWidth();
  166.                     alto = ""+imgObj.getHeight();
  167.                     sig = ""+imgObjSign.toString();
  168.                     System.out.println("mimeType: " + imgObj.getMimeType());
  169.                     System.out.println("height: " + imgObj.getHeight());
  170.                     System.out.println("width: " + imgObj.getWidth());
  171.                     System.out.println("contentLength: " + imgObj.getContentLength());
  172.                     System.out.println("contentFormat: " + imgObj.getContentFormat());
  173.                     System.out.println("compressionFormat: " + imgObj.getCompressionFormat());
  174.                     System.out.println("source type: " + imgObj.getSourceType());
  175.                     System.out.println("source loc: " + imgObj.getSourceLocation());
  176.                     System.out.println("source name: " + imgObj.getSourceName());
  177.                     System.out.println("source : " + imgObj.getSource());
  178.                                 is = imgObj.getContent().getBinaryStream();
  179.                         }
  180.                 }
  181.                 catch(Exception e)
  182.                 {
  183.                         System.out.println("exception raised " + e);
  184.                         e.printStackTrace();
  185.                 }
  186.                
  187.                 return is;
  188.         }
  189.  
  190.         public OracleConnection connect() throws Exception
  191.         {
  192.                 String connectString;
  193.                 Class.forName ("oracle.jdbc.driver.OracleDriver");
  194.                
  195.                 //connectString = "jdbc:oracle:oci8:@192.168.100.9:1521:prometeo";
  196.                
  197.                 connectString = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
  198.                 OracleConnection con = (OracleConnection)
  199.                 DriverManager.getConnection(connectString,"samariles","samariles");
  200.                 con.setAutoCommit(false);
  201.                 return con;
  202.         }
  203.  
  204. }