// Package com. redtroy;
Import javax. Media. player;
Import javax. Media. capturedeviceinfo;
Import javax. Media. medialocator;
Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. image .*;
Import java. AWT. event .*;
Import javax. Media. Control. framegrabbingcontrol;
Import javax. Media. buffer;
Import javax. Media. util. buffertoimage;
Import javax. Media. format. videoformat;
Import java. Io .*;
Import com.sun.image.codec.jpeg .*;
Import javax. Media. capturedevicemanager;
Import javax. Media. Manager;
Import java.net .*;
Import java. Applet .*;
// Import javax. Swing. Im
/**
* <P> title: </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2005 </P>
* <P> company: </P>
* @ Author not attributable
* @ Version 1.0
*/
Public class camera
Extends jframe {
Private Static player = NULL;
Private capturedeviceinfo device = NULL;
Private medialocator locator = NULL;
Private buffer = NULL;
Private buffertoimage b2i = NULL;
Private image;
Private imageicon iicon = new imageicon ();
Boolean proportion = true;
String str1 = "VFW: Logitech USB video camera: 0 ";
String str2 = "VFW: Microsoft WDM image capture (win32): 0 ";
Jbutton jbutton1 = new jbutton ();
Jbutton jbutton2 = new jbutton ();
Component component1;
Jlabel jlabel1 = new jlabel ();
Public camera (){
Super ("Troy camera (no save function )");
Try {
Jbinit ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
Public Image resize (INT width, int height, image source, Boolean flag ){
This. proportion = flag;
Int new_w;
Int new_h;
Toolkit TK = toolkit. getdefatooltoolkit ();
Applet APP = new applet ();
Mediatracker Mt = new mediatracker (APP );
Image IMG = source;
Try {
Mt. addimage (IMG, 0 );
Mt. waitforid (0 );
}
Catch (exception e ){
E. printstacktrace ();
}
If (IMG. getwidth (null) =-1 ){
System. Out. println ("can't read, retry! "+" <Br> ");
Return NULL;
}
Else {
If (this. proportion = true) {// determines whether it is proportional scaling.
// Calculate the width and height of the output image by proportional Scaling
Double rate1 = (double) IMG. getwidth (null)/(double) width +
0.1;
Double rate2 = (double) IMG. getheight (null)/(double) height +
0.1;
Double rate = rate1> rate2? Rate1: rate2;
New_w = (INT) (double) IMG. getwidth (null)/rate );
New_h = (INT) (double) IMG. getheight (null)/rate );
}
Else {
New_w = width; // The width of the output image.
New_h = height; // The height of the output image.
}
}
Bufferedimage buffimg = new bufferedimage (new_w, new_h,
Bufferedimage. type_int_rgb );
Graphics G = buffimg. creategraphics ();
G. setcolor (color. White );
G. fillrect (0, 0, new_w, new_h );
G. drawimage (IMG, 0, 0, new_w, new_h, null );
G. Dispose ();
Try {
Outputstream tempout = new
Fileoutputstream ("C: // temp.jpg ");
Required imageencoder encoder = required codec. createjpegencoder (tempout );
Encoder. encode (buffimg );
Tempout. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
Return Tk. createimage ("C: // temp.jpg ");
}
Public static void main (string [] ARGs ){
Camera camera1 = new camera ();
}
Private void jbinit () throws exception {
Component1 = Box. createglue ();
// ==================================== Initialize the device ===== //
Component1.addnotify ();
Device = capturedevicemanager. getdevice (str2 );
Locator = device. getlocator ();
Try {
Player = manager. createrealizedplayer (Locator );
Player. Start ();
If (component1 = player. getvisualcomponent ())! = NULL ){
This. getcontentpane (). Add (component1, null );
}
}
Catch (exception e ){
E. printstacktrace ();
}
Jbutton1.setbounds (New rectangle (294, 28, 73, 25 ));
Jbutton1.settext ("photo ");
Jbutton1.addactionlistener (New camera_jbutton?actionadapter (this ));
This. getcontentpane (). setlayout (null );
Jbutton2.setbounds (New rectangle (295, 82, 73, 25 ));
Jbutton2.settext ("save ");
Jbutton2.addactionlistener (New camera_jbutton2_actionadapter (this ));
Component1.setbounds (New rectangle (27, 23,243,235 ));
Jlabel1.seticontextgap (4 );
Jlabel1.settext ("null ");
Jlabel1.setverticaltextposition (swingconstants. center );
Jlabel1.setbounds (New rectangle (293,139, 80, 95 ));
This. getcontentpane (). Add (jbutton1, null );
This. getcontentpane (). Add (jbutton2, null );
This. getcontentpane (). Add (jlabel1, null );
This. setsize (400,300 );
This. setvisible (true );
}
Void jbutton?action=med (actionevent e ){
Iicon = new imageicon ();
Framegrabbingcontrol FGC = (framegrabbingcontrol) player. getcontrol (
"Javax. Media. Control. framegrabbingcontrol ");
Buffer = FGC. grabframe (); // obtain the current shard and save it to the buffer class
B2i = new buffertoimage (videoformat) buffer. getformat ());
Image = b2i. createimage (buffer); // show the image
Iicon = new imageicon ();
/* Toolkit TK = toolkit. getdefatooltoolkit ();
Applet APP = new applet ();
Mediatracker Mt = new mediatracker (APP );*/
Iicon. setimage (this. Resize (85, 95, image, true ));
Jlabel1.seticon (iicon );
}
Void jbutton2_actionreceivmed (actionevent e ){
Jfilechooser filechooser = new jfilechooser ();
Filechooser. setfilehidingenabled (true );
Filechooser. setfileselectionmode (jfilechooser. files_only );
Int result = filechooser. showsavedialog (null );
If (result = jfilechooser. cancel_option ){
Return;
}
File file = filechooser. getselectedfile ();
If (file = NULL | file. getname (). Equals ("")){
Joptionpane. showmessagedialog (null, "invalid file name", "warning ",
Joptionpane. error_message );
}
Else {
String S = file. getabsolutepath ();
Bufferedimage Bi = (bufferedimage) createimage (image. getwidth (null ),
Image. getheight (null ));
Graphics2d g2 = Bi. creategraphics ();
G2.drawimage (image, null, null );
Fileoutputstream out = NULL;
Try {
Out = new fileoutputstream (s );
}
Catch (Java. Io. filenotfoundexception Io ){
System. Out. println ("file not found ");
}
Required imageencoder encoder = required codec. createjpegencoder (out );
Export encodeparam Param = encoder. getdefaultjpegencodeparam (BI );
Param. setquality (1f, false); // do not compress the image
Encoder. setincluencodeparam (PARAM );
Try {
Encoder. encode (BI );
Out. Close ();
}
Catch (Java. Io. ioexception Io ){
System. Out. println ("ioexception ");
}
}
}
}
Class camera_jbutton?actionadapter
Implements java. AWT. event. actionlistener {
Camera adaptee;
Camera_jbutton?actionadapter (CAMERA adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton?action=med (E );
}
}
Class camera_jbutton2_actionadapter
Implements java. AWT. event. actionlistener {
Camera adaptee;
Camera_jbutton2_actionadapter (CAMERA adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton2_actionreceivmed (E );
}
}