0
\$\begingroup\$

I have a plane which contain a WebCamTexture and a button to capture the image enter image description here

script to capture the image

void CaptureAndSave () {
    Debug.Log (Camera_panel_script.webCameraTexture);
    Texture2D snap = new Texture2D(Camera_panel_script.webCameraTexture.width, Camera_panel_script.webCameraTexture.height);
    snap.SetPixels(Camera_panel_script.webCameraTexture.GetPixels());
    snap.Apply();
    Debug.Log (Application.persistentDataPath.ToString());
    System.IO.File.WriteAllBytes(
        Application.persistentDataPath+"/my_image.png",
        snap.EncodeToPNG()
        );
}

enter image description here

this is the resulted image

how can i modify my code to add the image logo to my resulted picture ? so the captured image would look like this :

enter image description here

EDIT: i tried to do a code to change the pixels in the of the webcameTexture like this :

public static Texture2D CombineTextures(GameObject obj, Texture2D background, Texture2D TodrawLogo)
{
    Vector3 v = obj.transform.position;// obj is TodrawLogo gameobject
    int width = TodrawLogo.width;
    int height = TodrawLogo.height;
    for (int x =(int)v.x; x < width; x++){
        background.SetPixel(x,(int)v.y,TodrawLogo.GetPixel(x,(int)v.y));
    }
    background.Apply();
    return background;
}

but i am out of luck also

\$\endgroup\$
2
\$\begingroup\$

I made some changes for your CombineTextures method,

public static Texture2D CombineTexture(GameObject obj, Texture2D background, Texture2D TodrawLogo)
{
    int width = TodrawLogo.width;
    int height = TodrawLogo.height;

    int backWidth = background.width;
    int backHeight = background.height;
    // bottom right corner
    int startX = backWidth - width;
    int startY = backHeight - height;
    // create new texture
    Texture2D t = new Texture2D(backWidth,backHeight);
    // right corner
    int y = 0;
    while (y < backHeight) {
        int x = 0;
        while (x < backWidth) {
            // set normal pixels
            background.SetPixel(x,y,background.GetPixel(x,y));
            // if we are at bottom right apply logo 
            //TODO also check alpha, if there is no alpha apply it!
            if(x >= startX && y < backHeight- startY)
                background.SetPixel(x,y,TodrawLogo.GetPixel(x-startX,y-startY));
            ++x;
        }
        ++y;
    }
    background.Apply();
    return background;
}

You can change the values inside while loop to place your texture where you want.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ You can get rid of the obj parameter as well since it does not seem to be needed. \$\endgroup\$ – Archimedes Trajano Jun 7 '17 at 4:06
0
\$\begingroup\$

You can easily achieve that effect by using a Second Camera.

Assuming that your Main Camera has Depth -1, add a second camera with Depth 0. Doing this will make the Second Camera render after the first.

You can then apply a specific layer Layer to your watermark object and then configure your Second Camera (Culling Mask property) to render only the watermark.

This way you will have your Main Camera render the whole scene, while the Second Camera will render only the watermark object, on top of what the Main Camera outputs.

As for the capturing part, try take a screenshot:

public class ExampleClass : MonoBehaviour {
    void OnMouseDown() {
        Application.CaptureScreenshot("Screenshot.png");
    }
}
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ can you check my edit please, my problem is not how i have to render the camera ,my problem is how should i save the watermark and the webcamtexture \$\endgroup\$ – Sora Feb 25 '15 at 9:06
  • \$\begingroup\$ My answer still stands. \$\endgroup\$ – Mihai-Andrei Dinculescu Feb 25 '15 at 9:08
  • \$\begingroup\$ but your answer is missing my point , the rendering is not the problem , the saving is the problem , as u can see i can save the captured image but without the watermark i need the final image to be the captured and on top of it the watermark as the image 3 show \$\endgroup\$ – Sora Feb 25 '15 at 9:10
  • \$\begingroup\$ I have updated my answer. Have you tried Application.CaptureScreenshot? \$\endgroup\$ – Mihai-Andrei Dinculescu Feb 25 '15 at 9:29
  • \$\begingroup\$ no i didn't because Application.CaptureScreenshot will make a screenshot for the whole view and by that the setting buttons and everything will be visible not online the camtexture and the watermark \$\endgroup\$ – Sora Feb 25 '15 at 9:31
0
\$\begingroup\$

Here's my version which uses Color32 rather than Color so we can utilize more integer math. My watermark was done in PNG which uses integers only. I also created an array of floats to prevent any floating point arithmetic when applying the watermark. And I used a premature optimization technique of going backwards in a loop but that's just my habit. This also handles transparency from an alpha channel.

// Initialize transparency map.  You can have this in a utility class
// if preferred
private static float[] TRANSPARENCY_MAP;

static MyClass() {
  TRANSPARENCY_MAP = new float[256];
  for (var i = 255 ; i >= 0; --i) {
    TRANSPARENCY_MAP[i] = (float)i/255;
  }
}

void AddWatermark(Texture2D target, Texture2D watermark) {
  // If your watermark is going to be reused a lot you may want to
  // store the GetPixels(), width and height as class variables.
  var watermarkPixels = watermark.GetPixels32();

  var pix = target.GetPixels32();

  for (var y = watermark.height - 1; y >= 0; --y) {
    for (var x = watermark.width - 1; x >= 0; --x) {
      var originInPix = 
        (y * target.width) + (target.width - watermark.width);
      var p1 = originInPix + x;
      var p2 = y * watermarkTexture.width + x;
      // Guard condition in case the watermark is larger than the image.
      if (p1 >= pix.Length || p2 >= watermarkPixels.Length)
      {
        continue;
      }
      var a = pix[p1];
      var b = watermarkPixels[p2];
      float l = TRANSPARENCY_MAP[b.a];
      pix[originInPix + x] = Color32.Lerp(a, b, l);
    }
  }
  target.SetPixels32(pix);
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.