Advertisement
Guest User

123

a guest
Jan 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.84 KB | None | 0 0
  1. /*
  2.  * $Id$
  3.  *
  4.  * Copyright (c) 1999-2007 Gnome spol. s r.o. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of
  7.  * Gnome spol. s r.o. You shall not disclose such Confidential
  8.  * Information and shall use it only in accordance with the terms
  9.  * of the license agreement you entered into with Gnome.
  10.  */
  11.  
  12. /**
  13.  *  SimpleExample demonstrates use of the Morena Framework in both
  14.  *  application and applet environment. Upload action cant be used
  15.  *  if it is invoked from local filesystem.
  16.  *  
  17.  *  Requirements:
  18.  *  1. Java2 1.4 or newer
  19.  *  2. Morena 6 for image acquisition
  20.  *  
  21. */
  22.  
  23. import java.awt.BorderLayout;
  24. import java.awt.Color;
  25. import java.awt.Container;
  26. import java.awt.Graphics;
  27. import java.awt.GridLayout;
  28. import java.awt.Image;
  29. import java.awt.Insets;
  30. import java.awt.Toolkit;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.MouseAdapter;
  33. import java.awt.event.MouseEvent;
  34. import java.awt.image.BufferedImage;
  35. import java.awt.image.ImageConsumer;
  36. import java.io.ByteArrayOutputStream;
  37. import java.io.File;
  38. import java.io.InputStream;
  39. import java.io.OutputStream;
  40. import java.net.HttpURLConnection;
  41. import java.net.URL;
  42.  
  43. import javax.imageio.ImageIO;
  44. import javax.swing.AbstractAction;
  45. import javax.swing.JApplet;
  46. import javax.swing.JFileChooser;
  47. import javax.swing.JFrame;
  48. import javax.swing.JOptionPane;
  49. import javax.swing.JPanel;
  50. import javax.swing.JTextField;
  51. import javax.swing.JToolBar;
  52. import javax.swing.WindowConstants;
  53. import javax.swing.border.LineBorder;
  54. import javax.swing.filechooser.FileFilter;
  55.  
  56. import SK.gnome.morena.Morena;
  57. import SK.gnome.morena.MorenaImage;
  58. import SK.gnome.morena.MorenaSource;
  59. import SK.gnome.twain.TwainSource;
  60. import SK.gnome.twain.TwainSource.Frame;
  61.  
  62.  
  63. public class MorenaStudio extends JApplet
  64. { private static class MainPanel extends JPanel
  65.   { private JTextField status = new JTextField();
  66.     private ImagePanel selected=null;
  67.     private SaveImageAction saveImageAction;
  68.     private UploadImageAction uploadImageAction;
  69.     private MouseListener mouseListener=new MouseListener();
  70.     private boolean hasServer=false;
  71.     private URL documentBase=null;
  72.  
  73.     private class RemoveAllAction extends AbstractAction implements Runnable
  74.     { RemoveAllAction()
  75.       { super("remove all");
  76.       }
  77.    
  78.       public synchronized void actionPerformed(ActionEvent event)
  79.       { new Thread(this).start();
  80.       }
  81.  
  82.       public synchronized void run()
  83.       { removeAll();
  84.         select(null);
  85.         repaint();
  86.       }
  87.     }
  88.  
  89.     private class AcquireImageAction extends AbstractAction implements Runnable
  90.     { AcquireImageAction()
  91.       { super("acquire image");
  92.       }
  93.    
  94.       public synchronized void actionPerformed(ActionEvent event)
  95.       { new Thread(this).start();
  96.       }
  97.  
  98.       public synchronized void run()
  99.       { try
  100.         { status.setText("Working ...");
  101.           MorenaSource source=Morena.selectSource(MainPanel.this);
  102.           if (source!=null)
  103.           { source.setColorMode();
  104.             source.setResolution(100);
  105.             while (true)
  106.             { MorenaImage morenaImage=new MorenaImage(source);
  107.               int imageStatus=morenaImage.getStatus();
  108.               if (imageStatus==ImageConsumer.STATICIMAGEDONE)
  109.               { int imageWidth=morenaImage.getWidth();
  110.                 int imageHeight=morenaImage.getHeight();
  111.                 int imagePixelSize=morenaImage.getPixelSize();
  112.                 ImagePanel image=new ImagePanel(Toolkit.getDefaultToolkit().createImage(morenaImage));
  113.                 MainPanel.this.add(image);
  114.                 select(image);
  115.                 int size=(int)Math.round(Math.sqrt(getComponentCount()));
  116.                 setLayout(new GridLayout(size, size));
  117.                
  118.                 if (TwainSource.class.isInstance(source))
  119.                 {
  120.                   Frame frame=((TwainSource)source).getFrame();
  121.                   status.setText("Done - actual image frame is ["+Math.round(100*frame.left)/100.0+", "+Math.round(100*frame.top)/100.0+"] - ["+Math.round(100*frame.right)/100.0+", "+Math.round(100*frame.bottom)/100.0+"] ...");
  122.                 }
  123.                 else
  124.                   status.setText("Done - actual image size is "+imageWidth+" x "+imageHeight+" x "+imagePixelSize+" ...");
  125.                 validate();
  126.                 if (TwainSource.class.isInstance(source) && ((TwainSource)source).hasMoreImages())
  127.                   continue;
  128.               }
  129.               else if (imageStatus==ImageConsumer.IMAGEABORTED)
  130.                 status.setText("Aborted, try again ...");
  131.               else if (imageStatus==ImageConsumer.IMAGEERROR)
  132.                 status.setText("Failed, try again ...");
  133.               break;
  134.             }
  135.           }
  136.           else
  137.             status.setText("Failed, try again ...");
  138.         }
  139.         catch (NoSuchMethodError error)
  140.         { JOptionPane.showMessageDialog(MainPanel.this, "Previous version of Morena is installed in "+System.getProperty("java.home")+".\nYou have to remove it first.\nClick OK to terminate browser.", "Error", JOptionPane.ERROR_MESSAGE);
  141.           System.exit(0);
  142.         }
  143.         catch (Throwable exception)
  144.         { JOptionPane.showMessageDialog(MainPanel.this, exception.toString(), "Error", JOptionPane.ERROR_MESSAGE);
  145.           exception.printStackTrace();
  146.           status.setText("Failed, try again ...");
  147.         }
  148.         finally
  149.         { try
  150.           { Morena.close();
  151.           }
  152.           catch (Exception exception)
  153.           { exception.printStackTrace();
  154.           }
  155.         }
  156.       }
  157.     }
  158.    
  159.     private class SaveImageAction extends AbstractAction implements Runnable
  160.     { private class Filter extends FileFilter
  161.       { String type;
  162.        
  163.         Filter(String type)
  164.         { this.type=type;
  165.         }
  166.        
  167.         public boolean accept(File file)
  168.         { return file.getName().endsWith(type);
  169.         }
  170.  
  171.         public String getDescription()
  172.         { return type+" Files";
  173.         }
  174.       }
  175.  
  176.       SaveImageAction()
  177.       { super("save to file");
  178.       }
  179.    
  180.       public void actionPerformed(ActionEvent event)
  181.       { new Thread(this).start();
  182.       }
  183.  
  184.       public synchronized void run()
  185.       { try
  186.         { status.setText("Working ...");
  187.           Image image=selected.getImage();
  188.           BufferedImage bufferedImage=new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
  189.           bufferedImage.createGraphics().drawImage(image, 0, 0, null);
  190.           JFileChooser chooser=new JFileChooser();
  191. //        Java 1.6 introduced a comfortable ImageIO.getWriterFileSuffixes() method.
  192.           String e[] = ImageIO.getWriterFormatNames();
  193.           for (int i=0; i<e.length; i++)
  194.             chooser.addChoosableFileFilter(new Filter(e[i]));
  195.           int result=chooser.showSaveDialog(MainPanel.this);
  196.           if (result==JFileChooser.APPROVE_OPTION)
  197.           { String ext=chooser.getFileFilter().getDescription();
  198.             ext=ext.substring(0, ext.indexOf(' ')).toLowerCase();
  199.             File file=chooser.getSelectedFile();
  200.             String name=file.getName();
  201.             if (!name.endsWith(ext))
  202.               file=new File(file.getParentFile(), name+"."+ext);
  203.             ImageIO.write(bufferedImage, ext, file);
  204.           }
  205.         }
  206.         catch (Throwable exception)
  207.         { JOptionPane.showMessageDialog(MainPanel.this, exception.toString(), "Error", JOptionPane.ERROR_MESSAGE);
  208.           exception.printStackTrace();
  209.           status.setText("Failed, try again ...");
  210.         }
  211.       }
  212.  
  213.       public boolean isEnabled()
  214.       { return selected != null;
  215.       }
  216.     }
  217.  
  218.     private class UploadImageAction extends AbstractAction implements Runnable
  219.     { UploadImageAction()
  220.       { super("upload to server");
  221.       }
  222.    
  223.       public void actionPerformed(ActionEvent event)
  224.       { new Thread(this).start();
  225.       }
  226.  
  227.       public synchronized void run()
  228.       { try
  229.         { status.setText("Working ...");
  230.           Image image=selected.getImage();
  231.           ByteArrayOutputStream tmp=new ByteArrayOutputStream();
  232.           BufferedImage bufferedImage=new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
  233.           bufferedImage.createGraphics().drawImage(image, 0, 0, null);
  234.           ImageIO.write(bufferedImage, "jpg", tmp);
  235.           tmp.close();
  236.           int contentLength=tmp.size();
  237.           if (contentLength>1024*1024)
  238.             throw new Exception("Image is too big to upload");
  239.           URL uploadURL=new URL(documentBase, "upload.php");
  240.           HttpURLConnection connection=(HttpURLConnection)uploadURL.openConnection();
  241.           connection.setRequestMethod("POST");
  242.           connection.setDoOutput(true);
  243.           connection.setUseCaches(false);
  244.           connection.setDefaultUseCaches(false);
  245.           connection.setRequestProperty("content-type", "img/jpeg");
  246.           connection.setRequestProperty("content-length", String.valueOf(contentLength));
  247.           OutputStream out=connection.getOutputStream();
  248.           out.write(tmp.toByteArray());
  249.           out.close();
  250.           InputStream in=connection.getInputStream();
  251.           int c;
  252.           while ((c=in.read())!=-1)
  253.             System.err.write(c);
  254.           in.close();
  255.           URL imageURL=new URL(documentBase, connection.getHeaderField("file-name"));
  256.           status.setText("Done - image is uploaded to "+imageURL+" (for at least 5 minutes) ...");
  257.         }
  258.         catch (Throwable exception)
  259.         { JOptionPane.showMessageDialog(MainPanel.this, exception.toString(), "Error", JOptionPane.ERROR_MESSAGE);
  260.           exception.printStackTrace();
  261.           status.setText("Failed, try again ...");
  262.         }
  263.       }
  264.  
  265.       public boolean isEnabled()
  266.       { return hasServer && selected != null;
  267.       }
  268.     }
  269.  
  270.     private class MouseListener extends MouseAdapter
  271.     { public void mouseClicked(MouseEvent event)
  272.       { select((ImagePanel)event.getComponent());
  273.       }
  274.     }
  275.    
  276.     private class ImagePanel extends JPanel
  277.     { private Image image;
  278.       int imageWidth;
  279.       int imageHeight;
  280.      
  281.       ImagePanel(Image image)
  282.       { this.image=image;
  283.         imageWidth=image.getWidth(null);
  284.         imageHeight=image.getHeight(null);
  285.         addMouseListener(mouseListener);
  286.       }
  287.      
  288.       public Image getImage()
  289.       { return image;
  290.       }
  291.      
  292.       public void paint(Graphics g)
  293.       { super.paint(g);
  294.         int panelWidth=getWidth()-6;
  295.         int panelHeight=getHeight()-6;
  296.         double horizontalRatio=(double)panelWidth/imageWidth;
  297.         double verticalRatio=(double)panelHeight/imageHeight;
  298.         if (horizontalRatio>verticalRatio)
  299.           g.drawImage(image, (int)(panelWidth-imageWidth*verticalRatio)/2+3, 3, (int)(imageWidth*verticalRatio), (int)(imageHeight*verticalRatio), this);
  300.         else
  301.           g.drawImage(image, 3, 3, (int)(imageWidth*horizontalRatio), (int)(imageHeight*horizontalRatio), this);
  302.       }
  303.  
  304.     }
  305.    
  306.     private class ToolBar extends JToolBar
  307.     { ToolBar()
  308.       { add(new RemoveAllAction());
  309.         addSeparator();
  310.         add(new AcquireImageAction());
  311.         addSeparator();
  312.         add(saveImageAction=new SaveImageAction());
  313.         saveImageAction.setEnabled(false);
  314.         addSeparator();
  315.         add(uploadImageAction=new UploadImageAction());
  316.         uploadImageAction.setEnabled(false);
  317.         setMargin(new Insets(4, 2, 2, 2));
  318.       }
  319.     }
  320.    
  321.     private class StatusBar extends JToolBar
  322.     { StatusBar()
  323.       { add(status);
  324.         status.setText("Ready ...");
  325.       }
  326.     }
  327.    
  328.     void select(ImagePanel image)
  329.     { if(selected != null)
  330.       selected.setBorder(null);
  331.       selected = image;
  332.       if(selected != null)
  333.       {
  334.         selected.setBorder(new LineBorder(Color.blue, 1));
  335.         saveImageAction.setEnabled(true);
  336.         uploadImageAction.setEnabled(hasServer);
  337.       } else
  338.       {
  339.         saveImageAction.setEnabled(false);
  340.         uploadImageAction.setEnabled(false);
  341.       }
  342.     }
  343.    
  344.     MainPanel(Container container, URL documentBase)
  345.     { this.documentBase=documentBase;
  346.       status.setEditable(false);
  347.       hasServer=documentBase!=null && documentBase.getProtocol().indexOf("http")!=-1;
  348.       container.add(new ToolBar(), BorderLayout.NORTH);
  349.       container.add(this, BorderLayout.CENTER);
  350.       container.add(status, BorderLayout.SOUTH);
  351.       setLayout(new GridLayout(1, 1));
  352.     }
  353.   }
  354.  
  355.   public void init()
  356.   { new MainPanel(getContentPane(), getDocumentBase());
  357.   }
  358.  
  359.   public static void main(String args[])
  360.   { JFrame frame=new JFrame("Morena Studio");
  361.     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  362.     new MainPanel(frame.getContentPane(), null);
  363.     frame.setBounds(100, 100, 600, 400);
  364.     frame.setVisible(true);
  365.   }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement