FM/Assets/Scripts/FUJIFILM/Other/TransparentCaptureSystem_FU...

125 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using UnityEngine;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;
namespace HK.FUJIFILM
{
public class TransparentCaptureSystem_FUJIFILM : TransparentCaptureSystem<TransparentCaptureSystem_FUJIFILM>
{
protected override void Init()
{
Instance = this;
base.Init();
}
void Update()
{
if (Input.GetKeyDown(captureKey))
{
CaptureAndCropNow();
}
}
[ContextMenu("立即截图并裁剪1")]
public override void CaptureAndCropNow(Action<byte[]> callback = null)
{
StartCoroutine(WaitCaptureAndCropFinished(callback));
}
public void PreviewCaptureAndCropFinished(Action<byte[]> callback = null)
{
StartCoroutine(WaitPreviewCaptureAndCropFinished(callback));
}
IEnumerator WaitPreviewCaptureAndCropFinished(Action<byte[]> callback = null)
{
var mugActualModelRenderFixed = uiRoot.GetComponent<MugActualModelRender_Fixed>();
var image = mugActualModelRenderFixed.designParent.GetChild(0).GetComponent<Image>();
image.enabled = true;
yield return StartCoroutine(CaptureAndCrop(callback));
Debug.Log("02 finished");
image.enabled = false;
}
public void DesignCaptureAndCropFinished(Action<byte[]> callback = null)
{
StartCoroutine(WaitDesignCaptureAndCropFinished(callback));
}
public IEnumerator WaitDesignCaptureAndCropFinished(Action<byte[]> callback = null)
{
var mugActualModelRenderFixed = uiRoot.GetComponent<MugActualModelRender_Fixed>();
var image = mugActualModelRenderFixed.designParent.GetChild(0).GetComponent<Image>();
image.enabled = false;
var verticalLayoutGroup = mugActualModelRenderFixed.designParent.GetComponent<VerticalLayoutGroup>();
verticalLayoutGroup.enabled = true;
// verticalLayoutGroup.childAlignment = TextAnchor.LowerLeft;
var tmpWidth = cropWidth;
var tmpHeight = cropHeight;
var screenshotSize = mugActualModelRenderFixed.productScriptableObject.screenshotSize;
cropWidth = (int)screenshotSize.x;
cropHeight = (int)screenshotSize.y;
cropOffset = new Vector2(0.5f, 0.5f);
yield return StartCoroutine(CaptureAndCrop(callback));
// 加载图像
using (var sImage = SixLabors.ImageSharp.Image.Load(FullPath))
{
// 等比缩放至宽度为 200高度自动计算以保持比例
sImage.Mutate(x => x.Resize(new ResizeOptions
{
Size = new Size(324, 204), // 高度设为 0 即自动按比例计算
Mode = ResizeMode.Max // 确保图像完全显示,不裁剪
}));
var directoryFullName = new FileInfo(FullPath).Directory.FullName;
// 保存为 JPEG设置质量
sImage.SaveAsPng($"{directoryFullName}/output.png", new PngEncoder());
}
// verticalLayoutGroup.childAlignment = TextAnchor.MiddleCenter;
verticalLayoutGroup.enabled = false;
cropWidth = tmpWidth;
cropHeight = tmpHeight;
cropOffset = new Vector2(0f, 0f);
Debug.Log("01 finished");
image.enabled = false;
}
IEnumerator WaitCaptureAndCropFinished(Action<byte[]> callback = null)
{
var mugActualModelRenderFixed = uiRoot.GetComponent<MugActualModelRender_Fixed>();
var image = mugActualModelRenderFixed.designParent.GetChild(0).GetComponent<Image>();
image.enabled = false;
yield return StartCoroutine(CaptureAndCrop());
Debug.Log("01 finished");
image.enabled = true;
// var tmpWidth = cropWidth;
// var tmpHeight = cropHeight;
// var screenshotSize = mugActualModelRenderFixed.productScriptableObject.screenshotSize;
// cropWidth = (int)screenshotSize.x;
// cropHeight = (int)screenshotSize.y;
yield return StartCoroutine(CaptureAndCrop(callback));
Debug.Log("02 finished");
// cropWidth = tmpWidth;
// cropHeight = tmpHeight;
image.enabled = false;
}
}
}