Commit e357ca6f authored by zwebrain's avatar zwebrain
Browse files

parent 3d14b92f
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
@@ -6656,6 +6656,7 @@ GameObject:
  - component: {fileID: 1465565644048445930}
  - component: {fileID: 2331007714620769148}
  - component: {fileID: 3403623617634526409}
  - component: {fileID: 8765985035443848586}
  m_Layer: 0
  m_Name: Geographic_Canvas
  m_TagString: Untagged
@@ -6776,6 +6777,18 @@ MonoBehaviour:
  m_EditorClassIdentifier: 
  eventsToReceive: 0
  debounceThreshold: 0.01
--- !u!114 &8765985035443848586
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 2664481979373483951}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 6d89580ebfc94563ad4b48db62e8f874, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
--- !u!1 &2667380975235082225
GameObject:
  m_ObjectHideFlags: 0
@@ -14803,9 +14816,9 @@ MonoBehaviour:
  m_OnClick:
    m_PersistentCalls:
      m_Calls:
      - m_Target: {fileID: 98861243989693998}
        m_TargetAssemblyTypeName: GeographyManager, Assembly-CSharp
        m_MethodName: OnClickBtn
      - m_Target: {fileID: 8765985035443848586}
        m_TargetAssemblyTypeName: PhotoManager, Assembly-CSharp
        m_MethodName: StartCapture
        m_Mode: 1
        m_Arguments:
          m_ObjectArgument: {fileID: 0}
+105 −0
Original line number Diff line number Diff line
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Script;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Windows.WebCam;


public class PhotoManager : MonoBehaviour
{
    private PhotoCapture photoCaptureObject;
    private const string UploadAddress = "http://10.20.48.171:10921/image/upload";
    private bool captureIsActive;

    public void StartCapture()
    {
        if (!captureIsActive)
        {
            Debug.Log("StartCapture...");
            captureIsActive = true;
            PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
        }
        else
        {
            captureIsActive = false;
        }
    }

    private void OnPhotoCaptureCreated(PhotoCapture captureobject)
    {
        photoCaptureObject = captureobject;
        var cameraResolution = PhotoCapture.SupportedResolutions
            .OrderByDescending(res => res.width * res.height)
            .First();
        var cameraParams = new CameraParameters()
        {
            hologramOpacity = 0f,
            cameraResolutionWidth = cameraResolution.width,
            cameraResolutionHeight = cameraResolution.height,
            pixelFormat = CapturePixelFormat.JPEG
        };

        captureobject.StartPhotoModeAsync(cameraParams, OnPhotoModeStarted);
    }

    private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
    {
        if (result.success)
        {
            photoCaptureObject.TakePhotoAsync((captureResult, frame) =>
                {
                    if (captureResult.success)
                    {
                        Debug.Log("Photo capture done.");

                        var buffer = new List<byte>();
                        frame.CopyRawImageDataIntoBuffer(buffer);
                        StartCoroutine(this.UploadImageCaptured(buffer.ToArray()));
                    }

                    photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
                }
            );
        }
        else
        {
            Debug.LogError("Unable to start photo mode!");
        }
    }

    private void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
    {
        photoCaptureObject.Dispose();
        photoCaptureObject = null;
        captureIsActive = false;
    }

    public IEnumerator UploadImageCaptured(byte[] imageBytes)
    {
        WWWForm wwwForm = new WWWForm();

        wwwForm.AddBinaryData("file", imageBytes, "photo.jpg");
        wwwForm.AddField("latitude", User.GetInstance().Latitude.ToString());
        wwwForm.AddField("longitude", User.GetInstance().Longitude.ToString());

        using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(UploadAddress, wwwForm))
        {
            unityWebRequest.downloadHandler = new DownloadHandlerBuffer();

            yield return unityWebRequest.SendWebRequest();

            if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
            {
                Debug.Log(unityWebRequest.error);
            }
            else
            {
                string response = unityWebRequest.downloadHandler.text;
                Debug.Log(response);
            }
        }
    }
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 6d89580ebfc94563ad4b48db62e8f874
timeCreated: 1638180718
 No newline at end of file