How to get photo image and work with it
I need help on this question: how can I get photo from the camera on Android, bring it to the scene on background and set some objects on the foreground?
I have a script that displays the camera on the RawImage UI object and I can get the texture of this object. But the problem is that RawImage always stands on the foreground. Tried to just make a screenshot of the phone screen, store in the app directory, but I can;t pick it up and work with this way. And you cann't take a texture on a regular UI-Image object, in fact, because there are only sprites accepted.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PhoneCamera : MonoBehaviour {
private bool camAvaliable;
private WebCamTexture backCam;
private Texture defaultBackgound;
private Texture main;
public bool check=false;
public RawImage background;
public AspectRatioFitter fit;
public Texture2D virtualPhoto;
public GameObject photo;
public bool try_photo = false;
[SerializeField] private GameObject img;
// Use this for initialization
void Start () {
defaultBackgound = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if(devices.Length == 0)
{
Debug.Log("No camera");
camAvaliable = false;
return;
}
for (int i = 0; i < devices.Length; i++)
{
if (!devices[i].isFrontFacing)
{
backCam = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
}
}
if (backCam == null)
{
Debug.Log("cam not found");
return;
}
backCam.Play();
background.texture = backCam;
camAvaliable = true;
}
public void OnShoot()
{
// virtualPhoto = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// virtualPhoto.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
// virtualPhoto.Apply();
// string imagePath = Application.persistentDataPath + "/SavedScreen_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
// Debug.Log(imagePath);
// byte[] bytes = virtualPhoto.EncodeToJPG();
// System.IO.File.WriteAllBytes(imagePath, bytes);
check = true;
}
// Update is called once per frame
void Update () {
if(check)
{
photo.SetActive(true);
try_photo = true;
// SceneManager.LoadScene(0);
}
else
{
photo.SetActive(false);
try_photo = false;
}
if (!camAvaliable)
return;
float ratio = (float)backCam.width / (float)backCam.height;
fit.aspectRatio = ratio;
float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
int orient = -backCam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
}
It will probably be convenient for me to work with this in OnShoot so that when you take a photo you can work with it.
Tell me, please, how to get the normal background of the picture.
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612132509im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Boolean variable is never true, even if declared true or ticked in the inspector 1 Answer
How I can make my camera keep a distance between two objects? 0 Answers
Set the rotation axis of the camera 0 Answers
Create a .png file at runtime and export. 1 Answer
How to do a wall jump? i am very novice 0 Answers