JAVA b/S through the camera to capture picture information solutions.

Source: Internet
Author: User

Call the camera on the browser. There's Activx,flash,html5,java. One of the ActiveX only support IE not to consider, HTML5 low version of the browser does not support the same waiver, the only flash left. Java to be re-developed, no time. Online search for a long time, finally found a jquery plug-in can achieve the transfer video. On the code:

1. Download the relevant documents on the website

http://www.xarg.org/project/jquery-webcam-plugin/

2. Front-End Footsteps:

<script>$(function() {    varSwfpah= "${pagecontext.request.contextpath}/resources/plugins/jquery-webcam-master/jscam_canvas_only.swf";//Flash Connection    varpos = 0, CTX =NULL, SAVECB, image = []; varw=320; varh=240; varCanvas = document.createelement ("Canvas"); Canvas.setattribute (' Width ', W); Canvas.setattribute (' Height ', h); if(1==0&&canvas.todataurl) {//support HTML5 time withDebugger; CTX= Canvas.getcontext ("2d"); Image= Ctx.getimagedata (0, 0, W, h); SAVECB=function(data) {//Will loop the method until each line is read, and data is the coordinate of the line. varCol = Data.split (";"); varIMG =image;  for(vari = 0; I < 320; i++) {varTMP =parseint (Col[i]); Img.data[pos+ 0] = (tmp >> +) & 0xFF; Img.data[pos+ 1] = (tmp >> 8) & 0xFF; Img.data[pos+ 2] = tmp & 0xFF; Img.data[pos+ 3] = 0xFF; POS+ = 4; }            if(POS >= 4 * w *h) {Ctx.putimagedata (IMG,0, 0); $.ajax ({URL:".. /test2/video ", DataType:"JSON", type:' Post ', data:{type:"Data", Image:canvas.toDataURL ("Image/png")}, Error:function(e) {}, Beforesend:function() {layer.load (); }, Complete:function() {Layer.closeall (' Loading '); }, Success:function(URL) {$ (' #myImg '). attr (' src '), URL); }                })            //$.post (".. /test2/video?tmp= "+ math.random (), {type:" Data ", Image:canvas.toDataURL (" Image/png ")},function (URL)            //            {            //$ (' #myImg '). attr (' src ', url);            //}, "json");pos = 0;    }        }; } Else{SAVECB=function(data) {//Loop call the method Image.push (data); POS+ = 4 *W; if(POS >= 4 * w *h) {$.ajax ({URL:".. /test2/video ", DataType:"JSON", type:' Post ', data:{type:"Pixel", Image:image.join (|), W:w, h:h}, Error:function(c) {}, Beforesend:function() {layer.load (); }, Complete:function() {Layer.closeall (' Loading '); }, Success:function(URL) {$ (' #myImg '). attr (' src '), URL); }                })            //$.post (".. /test2/video ", {type:" Pixel ", Image:image.join (' | '), w:w,h:h},function (URL)            //            {            //alert (URL);            //$ (' #myImg '). attr (' src ', url);            //}, "json");pos = 0;    }        }; }      $("#webcam"). Webcam ({width:w, height:h, mode:"Callback", Swffile:swfpah, ONSAVE:SAVECB, Oncapture:function() {webcam.save (); }, Debug:function(Type, string) {Console.log (type+ ": " +string); }, OnLoad:function() {            $(' #test '). Bind (' click ',function() {webcam.capture ();        }); }    });}); </script>

HTML code:

1 <DivID= "Webcam">       2    </Div>3    <ahref= "javascript:webcam.capture (); void (0);">Take a picture instantly</a>4    <inputID= "Test"type= "button"value= "Test" />5    <imgID= "Myimg"alt=""src="">6 7 //webcam After a successful load, there will be a global object webcam. The intrinsic method of the object:8 capture ([delay])9 Captures an image internally.Ten save ([file]) One Saves the captured image accordingly to the storage mode. A getcameralist () - Get ' s An array of available cameras. If no camera is a installed, an error is thrown and a empty array is returned. - Setcamera ([index]) theSwitches to a different camera. The parameter is the index of the element in the resulting array of getcameralist ()

Java Backend Code:

@RequestMapping (value = "/video", method = requestmethod.post)        publicthrows   IOException, documentexception {            = request.getparameter ("type");             = Request.getparameter ("image");

if (Stringutils.isempty (LX))
Return "";
String image = Request.getparameter ("image");
if (Stringutils.isempty (image))
Return "";

BooleanR=false; String UUID=Uuid.randomuuid (). toString (); String URL= Request.getcontextpath () + "\\resources\\PDF\\tmp\\" + UUID + ". png"; String Outpath= Request.getrealpath ("/") + "\\resources\\PDF\\tmp\\" + UUID + ". png"; if(Lx.equals ("Data") ) {R=base64toimg (image, Outpath); }            if(Lx.equals ("Pixel") ) {String W= Request.getparameter ("W"); String h= Request.getparameter ("h"); R=base32toimg (W,h,image,outpath); }            if(R)returnURL; Else            return""; } @SuppressWarnings ("Restriction")//resolves a 64-bit format Public Booleanbase64toimg (String imgstr, String imgfilepath) {if(Imgstr = =NULL) {            return false; } imgstr=imgstr.replace ("Data:image/png;base64," "," "). Trim (); Base64decoder Decoder=NewBase64decoder (); Try {            byte[] bytes =Decoder.decodebuffer (IMGSTR);  for(inti = 0; i < bytes.length; ++i) {if(Bytes[i] < 0) {//Adjust Exception DataBytes[i] + = 256; }} OutputStream out=NewFileOutputStream (Imgfilepath);            Out.write (bytes);            Out.flush ();            Out.close (); return true; } Catch(Exception e) {e.printstacktrace (); return false; }}//to parse an array of ordinary coordinates Public Booleanbase32toimg (String width,string height,string imgstr,string imgfilepath) {if(Imgstr = =NULL) {         return false; }    intW =integer.parseint (width); inth =integer.parseint (height); Try{bufferedimage bf=NewBufferedImage (W, H, Bufferedimage.type_int_rgb); string[] Rows= Imgstr.split ("\\|");  for(inti = 0; i < rows.length; i++) {string[] col= Rows[i].split (";");  for(intj = 0; J < Col.length; J + +) {                intdata = Integer.parseint (Col[j], 10);            Bf.setrgb (J, I, data); }} File outfile=NewFile (Imgfilepath); if(Outfile.exists ()) {outfile.delete (); } imageio.write (BF,"PNG", outfile); } Catch(Exception e) {e.printstacktrace (); return false; }    return true;}

JAVA b/S through the camera to capture picture information solutions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.