Unity3d call external camera, save picture, do not use screenshot mode

Source: Internet
Author: User

First of all, thank the seniors for using screenshots to call the external camera and save the picture: http://blog.csdn.net/a112634313/article/details/8472786

Texture2d processing http://www.narkii.com/club/thread-291958-1.html

Not much to say directly on my code

Using Unityengine; Using System.Collections; Using System.IO; public class Cameratest:monobehaviour {public Gameobject obj;  Webcamtexture cameratexture;  String Cameraname= "";  private bool Isplay = false;  Use the this for initialization void Start () {Startcoroutine (Test ());  }//update is called once per frame void Update () {obj.renderer.material.maintexture=cameratexture;}  IEnumerator Test () {yield return application.requestuserauthorization (USERAUTHORIZATION.WEBCAM);  if (Application.hasuserauthorization (Userauthorization.webcam)) {webcamdevice[] devices = webcamtexture.devices;  Cameraname = Devices[0].name;  Cameratexture = new Webcamtexture (Cameraname, 400, 300, 15);  Cameratexture.play ();  Isplay = true; }} void Ongui () {if (isplay) {GUI.  Drawtexture (New Rect (0, 0, N, a), cameratexture, Scalemode.scaletofit); } if (GUI. button (new Rect (400,0,450,50), "Save")) {Startcoroutine (gettexture2d ());} if (GUI. button (new Rect (400,50,450,50), "AR")) {application.Loadlevel ("HelloWorld"); Cameratexture.stop ();}} IEnumerator gettexture2d () {yield return new waitforendofframe ();//texture2d savedtexture;//To save the size of the picture//intercept area Texture2d Savedtexture=obj.renderer.material.maintexture as texture2d;//mode/* texture2d t = new texture2d (200, 200);//To save the size of the picture// Intercept the area T. Readpixels (New Rect (0,cameratexture.height, 0, 0), t.apply ();//Convert picture data to a byte array byte[] Byt = t. Encodetopng ();//Then Save as Picture File.writeallbytes (Application.datapath + "/streamingassets/tutorialhelloworld/" + " Target1.png ", byt);}    */texture2d texture = new Texture2d (cameratexture.width, cameratexture.height); int y = 0;    while (Y < texture.height) {int x = 0;    while (x < texture.width) {color color = cameratexture.getpixel (x, y); Texture.      SetPixel (x, y, color);      ++x;    } ++y; } texture.    Apply (); byte[] Pngdata = texture. Encodetopng (); File.writeallbytes (Application.datapath + "/streamingassets/tutorialhelloworld/" + "Target1.png", PngData);}}   

To illustrate, to add a plane to the scene, my goal is to put the camera as a sticker on the panel to display in real time, because I foundWebcamtexture cannot be used directly as a texture2d

Then click the Save button to save the picture to the specified path: Note that the path can be modified, but it does not create itself, will be error. Look at the results



What is the benefit of doing this compared to the screenshot, I think:

1, the screen coordinates need your own adjustment, Unity3d the default coordinate origin and the screen when the coordinate origin is inconsistent, one in the upper left corner, the adjustment is very troublesome

2. Screenshot method Once the screen resolution changes then your program will change accordingly, otherwise it will be a problem

3, this way can be placed directly on the plane or any texture2d, there is a great freedom, and do not need to worry about whether the picture is complete, there is no other impurities, what the camera gets, then save the picture is what.

Unity3d call external camera, save picture, do not use screenshot mode

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.