第一步:下载视频
第二部:不要弹幕,利用KMPlayer高级版本,视频捕捉功能,截取帧图片,按顺序编号
第三部:利用Unity获取本地图片,判断图片像素color.r * 255) > 200,获得“0” “1” 或者 “ ”“*”这种类似的编码
第四部:把“0” “1” 按帧序列的速度,利用UI显示出来
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.UI;
using System.IO;
using LitJson;
using UnityEditor;
using System.Text;
using System;
using System.Timers;
using System.IO.Compression;
public class BadApple : MonoBehaviour
{
public MovieTexture movTexture;
private List<string> Alltextures = new List<string>();
public UITexture myTexture;
public UILabel myLabel;
private float m_fFps = 4;
private int mytime = 0;
private bool IsShow = false;
private StreamReader reader;
/// <summary>
///获取本地图片
/// </summary>
void GetTextureOneByOne()
{
string[] path = Directory.GetFileSystemEntries("E:\\Program Files (x86)\\Kmplayer Plus\\Cap");
//保存为Gzip压缩的文本流
FileStream fileStreamAll = new FileStream("badappleTest.txt", FileMode.Create, FileAccess.Write);
// GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);//压缩技术
StreamWriter sw = new StreamWriter(fileStreamAll);
for (int n = 000251; n < 000299; n++)
{
string name = n.ToString();
name = name.Length > 3 ? "00" + name : "000" + name;
//创建文件读取流
FileStream fileStream = new FileStream("E:\\Program Files (x86)\\Kmplayer Plus\\Capture\\ba" + name + ".jpg", FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//创建Texture
int width = 960;
int height = 720;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
//for (int i = 0; i < texture.width; i+=10)
//{
// for (int j = 0; j < texture.height; j+=10)
// {
// Color color = texture.GetPixel(i,j);
// if ((int)(color.r * 255) > 200)
// {
// //Console.Write("1");
// sw.Write(" ");
// }
// else
// {
// //Console.Write("0");
// sw.Write("#");
// }
// }
// //Console.WriteLine();
// sw.WriteLine();
// Console.Clear();
// Console.WriteLine("{0} of {1} completed! ", 1, path.Length);
//}
string labels = "";
for (int i = 0; i < texture.width; i += 10)
{
for (int j = 0; j < texture.height; j += 10)
{
Color color = texture.GetPixel(i, j);
if ((int)(color.r * 255) > 200)
{
//Console.Write("1");
labels = labels + "1";
}
else
{
//Console.Write("0");
labels = labels + "0";
}
}
}
Alltextures.Add(labels);
}
}
void Start()
{
GetTextureOneByOne();
IsShow = true;
// GetTextures();
// ShowTextures();
//电影纹理
renderer.material.mainTexture = movTexture;
//设置电影纹理播放模式为循环
movTexture.loop = true;
movTexture.Play();
}
void GetTextures()
{
//获取in目录下所有文件 未作异常处理
string[] path = Directory.GetFileSystemEntries("E:\\Program Files (x86)\\Kmplayer Plus\\Cap");
//保存为Gzip压缩的文本流
FileStream fileStream = new FileStream("badapple.txt", FileMode.Create, FileAccess.Write);
// GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);//压缩技术
StreamWriter sw = new StreamWriter(fileStream);
//遍历每个像素点
for (int x = 0; x < path.Length; x++)
{
//Console.WriteLine(path[x]);
Texture2D bitmap = new Texture2D(970,720);
bitmap.LoadImage(System.Text.Encoding.Default.GetBytes(path[x]));//string类型转成byte[]:
Debug.Log(bitmap.height + " " + bitmap.width);
for (int i = 0; i < bitmap.height; i++)
{
for (int j = 0; j < bitmap.width; j++)
{
Color color = bitmap.GetPixel(j, i);
if (color.r > 200)
{
//Console.Write("1");
sw.Write(" ");
}
else
{
//Console.Write("0");
sw.Write("#");
}
}
//Console.WriteLine();
sw.WriteLine();
Console.Clear();
Console.WriteLine("{0} of {1} completed! ", x, path.Length);
}
}
sw.Dispose();
}
void ShowTextures()
{
//打开压缩文本流
FileStream fileStream = new FileStream("badapple.dat", FileMode.Open, FileAccess.Read);
// GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress);
reader = new StreamReader(fileStream);
IsShow = true;
}
void ShowMovieOld()
{
}
void Update()
{
if (IsShow)
{
int index = (int)(Time.time * m_fFps) % Alltextures.Count; //数组的索引,根据时间改变,当前时间乘以fps与总帧数取余,就是播放的当前帧,随着update更新
//myTexture.mainTexture = textures[index]; //渲染这个贴图
myLabel.text = Alltextures[index];
}
}
void noOnGUI()
{
// for (int i = 1; i < 73; i++)
// {
GUI.Label(new Rect(100, 100, 960, 10), "#############+&##############/&##########");
// }
GUI.Label(new Rect(100, 100, 960, 10 * 50), "#############+&##############/&##########");
}
void noStart()
{
//Debug.Log(textures.Count);
//int length = textures.Count;
// StringBuilder sb;
// StreamWriter sw;
// JsonWriter writer;
//string filePath = Application.dataPath + "/StreamingAssets/ badapple.txt";
//FileInfo t = new FileInfo(filePath);
//if (!File.Exists(filePath))
//{
// File.Delete(filePath);
//}
//sw = t.CreateText();
//sb = new StringBuilder();
//writer = new JsonWriter(sb);
// writer.WriteArrayStart();
//for (int i = 0; i < length; i++)
//{
// writer.WriteObjectStart();
// Texture2D bitmap = textures[i];
// int width = bitmap.width;
// int heigh = bitmap.height;
// for (int x = 0; x < width; x++)
// {
// for (int y = 0; y < heigh; y++)
// {
// writer.WritePropertyName(x+","+y);
// writer.Write(bitmap.GetPixel(x, y).r>200?"#":" ");
// }
// }
// writer.WriteObjectEnd();
//}
// writer.WriteArrayEnd();
// sw.WriteLine(sb.ToString());
// sw.Close();
// sw.Dispose();
}
}