public void createPhoto(List<string> filename, int time) { if (filename.Count == 0) return; string[] profile = profilePath.Split ('\\'); string[] root = null; if(ResourceManager.homeDirectory_ != null) root = ResourceManager.homeDirectory_.Split('\\'); //もしコマンドラインから起きたら、profile.Length > 1。 if (profile.Length > 1 && time > 0) { //List<string> paths = new List<string>(); string[] parts = filename[0].Split('\\'); if (parts.Length <= root.Length) { MessageBox(new IntPtr(0), string.Format("連携モード時は、{0}配下のファイルのみ指定できます。", ResourceManager.homeDirectory_), "Error Reading Photo", 0); return; } bool flag = false; for (int i = 0; i < root.Length; i++) { if (!string.Equals(parts[i], root[i], StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } if (flag) { MessageBox(new IntPtr(0), string.Format("連携モード時は、{0}配下のファイルのみ指定できます。", ResourceManager.homeDirectory_), "Error Reading Photo", 0); return; } } //photos = new List<Photo>(); progressBar = new ProgressBarForm(); progressBar.Location = new System.Drawing.Point((int)SystemParameter.clientBounds.Min.X, (int)SystemParameter.clientBounds.Min.Y); progressBar.Begin(filename.Count); for(int i = 0; i < filename.Count; i++) { PhotoTag ptag = new PhotoTag(); Photo.colorFeature color = new Photo.colorFeature(); PhotoLog nowLog = new PhotoLog(filename[i]); string[] namePart = filename[i].Split('\\'); Texture2D t; if (time == 0) { root = ResourceManager.homeDirectory_.Split('\\'); if (namePart.Length <= root.Length) { MessageBox(new IntPtr(0), string.Format("連携モード時は、{0}配下のファイルのみ指定できます。", ResourceManager.homeDirectory_), "Error Reading Photo", 0); continue; } bool flag = false; for (int j = 0; j < root.Length; j++) { if (!string.Equals(namePart[j], root[j], StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } if (flag) { MessageBox(new IntPtr(0), string.Format("連携モード時は、{0}配下のファイルのみ指定できます。", ResourceManager.homeDirectory_), "Error Reading Photo", 0); continue; } } if (File.Exists(filename[i])) { try { using (FileStream fileStream = new FileStream(filename[i], FileMode.Open)) { t = Texture2D.FromStream(SystemParameter.graphicsDevice, fileStream); //t = new Texture2D(SystemParameter.graphicsDevice, SystemParameter.graphicsDevice.PresentationParameters.BackBufferHeight, SystemParameter.graphicsDevice.PresentationParameters.BackBufferWidth, true, SystemParameter.graphicsDevice.PresentationParameters.BackBufferFormat); //t.SetData<FileStream>(2, } } catch (Exception e) { MessageBox(new IntPtr(0), e.ToString(), "Error Reading Photo", 0); continue; } } else continue; Photo newP = null; //if tag exists if (photoLog.ContainsKey(filename[i])) { nowLog = photoLog[filename[i]]; ptag.CapturedDate = PhotoLog.String2DateTime(PhotoLog.FormatTimeStamp(nowLog.CapturedTimeStamp)); ptag.CreatedDate = PhotoLog.String2DateTime(PhotoLog.FormatTimeStamp(nowLog.CreateTimeStamp)); //read tag string[] sepName = new string[1]; sepName[0] = "\\"; string[] names = filename[i].Split(sepName, StringSplitOptions.RemoveEmptyEntries); string name = names[names.Length - 1]; sepName[0] = ","; string[] nameTags = name.Split(sepName, StringSplitOptions.RemoveEmptyEntries); for (int j = 1; j < nameTags.Length - 1; j++) { List<string> tagList; tagList = nowLog.Tags.ToList(); if (nowLog.Tags == null || !nowLog.Tags.Contains(nameTags[j])) { tagList.Add(nameTags[j]); } nowLog.Tags = tagList.ToArray(); } if (nowLog.Tags != null && nowLog.Tags.Length > 0) ptag.allTags = nowLog.Tags.ToList(); if (nowLog.Feature != null) { List<Vector3> f = new List<Vector3>(); for (int j = 0, len = nowLog.Feature.Length; j < len; j += 3) { f.Add(new Vector3(float.Parse(nowLog.Feature[j]), float.Parse(nowLog.Feature[j + 1]), float.Parse(nowLog.Feature[j + 2]))); } color.feature_ = f.ToArray(); color.variance_ = double.Parse(nowLog.Variance); } else { color = Photo.CalcFeature(t); List<string> f = new List<string>(); for (int j = 0, len = color.feature_.Length; j < len; ++j) { f.Add(color.feature_[j].X.ToString()); f.Add(color.feature_[j].Y.ToString()); f.Add(color.feature_[j].Z.ToString()); } nowLog.Feature = f.ToArray(); nowLog.Variance = color.variance_.ToString(); photoLog[nowLog.FilePath] = nowLog; } } else { //file name to tag string[] sepName = new string[1]; sepName[0] = "\\"; string[] names = filename[i].Split(sepName, StringSplitOptions.RemoveEmptyEntries); string name = names[names.Length - 1]; sepName[0] = ","; string[] nameTags = name.Split(sepName, StringSplitOptions.RemoveEmptyEntries); for (int j = 1; j < nameTags.Length - 1; j++) { List<string> tagList; tagList = nowLog.Tags.ToList(); if (nowLog.Tags == null || !nowLog.Tags.Contains(nameTags[j])) { tagList.Add(nameTags[j]); } nowLog.Tags = tagList.ToArray(); } if (nowLog.Tags != null && nowLog.Tags.Length > 0) ptag.allTags = nowLog.Tags.ToList(); //char[] sep = { '/', ' ', ':'}; char[] sep = { '/', ' ', ':', '-' }; // modified by Gengdai, 2011-11-9 string nowtime = DateTime.Now.ToString(); nowLog.CreateTimeStamp = nowtime.Split(sep, StringSplitOptions.RemoveEmptyEntries); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(filename[i]); int[] pidList = bmp.PropertyIdList; int index = Array.IndexOf(pidList, 0x9003); if (index == -1) { DateTime WriteTimeStamp = File.GetLastWriteTime(filename[i]); if (DateTime.Compare(WriteTimeStamp, PhotoLog.String2DateTime(nowLog.CreateTimeStamp)) < 0) { nowLog.CapturedTimeStamp = (WriteTimeStamp.ToString()).Split(sep, StringSplitOptions.RemoveEmptyEntries); } else { nowLog.CapturedTimeStamp = nowLog.CreateTimeStamp; } } else { try { System.Drawing.Imaging.PropertyItem pi = bmp.PropertyItems[index]; string cts = Encoding.ASCII.GetString(pi.Value, 0, 19); nowLog.CapturedTimeStamp = cts.Split(sep, StringSplitOptions.RemoveEmptyEntries); } catch(Exception e) { Console.WriteLine("read capture time error"); MessageBox(new IntPtr(0), "capture time read error!", "Capture Time Error", 0); nowLog.CapturedTimeStamp = nowLog.CreateTimeStamp; } } bmp.Dispose(); ptag.CapturedDate = PhotoLog.String2DateTime(nowLog.CapturedTimeStamp); ptag.CreatedDate = PhotoLog.String2DateTime(nowLog.CreateTimeStamp); //calculate color feature color = Photo.CalcFeature(t); List<string> f = new List<string>(); for (int j = 0, len = color.feature_.Length; j < len; ++j) { f.Add(color.feature_[j].X.ToString()); f.Add(color.feature_[j].Y.ToString()); f.Add(color.feature_[j].Z.ToString()); } nowLog.Feature = f.ToArray(); nowLog.Variance = color.variance_.ToString(); photoLog[nowLog.FilePath] = nowLog; } //problem: two images cannot have the same filename newP = new Photo(i, filename[i], new Vector2( (float)(random_.NextDouble() * SystemParameter.ClientWidth), (float)(random_.NextDouble() * SystemParameter.ClientHeight)), (float)(random_.NextDouble() * 0.1) + 0.25f, 0f, ptag, t, color); photos.Add(newP); // people tag: added by Gengdai if (peopleTags.ContainsKey(filename[i])) { var p = peopleTags[filename[i]]; newP.PTags = p; // add tags directly foreach (PeopleTag pt in p.pTags) { newP.ptag.allTags.Add(pt.People); } } //photos[photos.Count - 1].SetTexture(t); newP.Center = new Vector2(t.Width, t.Height) * 0.5f; newP.BoundingBox = new BoundingBox2D(newP.Position - newP.Center * newP.Scale - Vector2.One * (float)ResourceManager.MAR, newP.Position + newP.Center * newP.Scale + Vector2.One * (float)ResourceManager.MAR, newP.Angle); progressBar.ProgressName(); } progressBar.End(); }
public void createPhoto(List<string> filename) { //filename indicates the whole directory, names contains only filename savePhotoLogs(); photos.Clear(); peopleTags.Clear(); photoLog.Clear(); ReadPhotoLogs(); //photos = new List<Photo>(); progressBar = new ProgressBarForm(); progressBar.Location = new System.Drawing.Point((int)SystemParameter.clientBounds.Min.X, (int)SystemParameter.clientBounds.Min.Y); //List<String> names = new List<string>(); //foreach (String file in filename) //{ // String[] tempText = file.Split(new Char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); // tempText = tempText[tempText.Length - 1].Split(new Char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); // names.Add(tempText[0]); //} //Dictionary<string, PhotoTag> tags = table.select(names); //Dictionary<string, Photo.colorFeature> colors = colorTable.select(names); //Dictionary<string, Photo.colorFeature> newColors = new Dictionary<string, Photo.colorFeature>(); progressBar.Begin(filename.Count); for(int i = 0; i < filename.Count; i++) { PhotoTag ptag = new PhotoTag(); Photo.colorFeature color = new Photo.colorFeature(); PhotoLog nowLog = new PhotoLog(filename[i]); string[] namePart = filename[i].Split('\\'); Texture2D t; //if(rootPath.Length != namePart.Length) // continue; if (File.Exists(filename[i])) { using (FileStream fileStream = new FileStream(filename[i], FileMode.Open)) { t = Texture2D.FromStream(SystemParameter.graphicsDevice, fileStream); //t = new Texture2D(Browser.Instance.GraphicsDevice, 300, 200); } } else { MessageBox(new IntPtr(0), string.Format("Doesn't exist photo: {0}", filename[i]), "Error Reading Photo", 0); return; } Photo newP = null; //if tag exists if (photoLog.ContainsKey(filename[i])) { nowLog = photoLog[filename[i]]; // 添加照片标签对象 ptag.CapturedDate = PhotoLog.String2DateTime(PhotoLog.FormatTimeStamp(nowLog.CapturedTimeStamp)); ptag.CreatedDate = PhotoLog.String2DateTime(PhotoLog.FormatTimeStamp(nowLog.CreateTimeStamp)); if (nowLog.Tags.Length > 0) { ptag.allTags = nowLog.Tags.ToList<string>(); } if (nowLog.Feature != null) { List<Vector3> f = new List<Vector3>(); for (int j = 0, len = nowLog.Feature.Length; j < len; j += 3) { f.Add(new Vector3(float.Parse(nowLog.Feature[j]), float.Parse(nowLog.Feature[j + 1]), float.Parse(nowLog.Feature[j + 2]))); } color.feature_ = f.ToArray(); } color.variance_ = double.Parse(nowLog.Variance); } else { //file name to tag string[] sepName = new string[1]; sepName[0] = "\\"; string[] names = filename[i].Split(sepName, StringSplitOptions.RemoveEmptyEntries); string name = names[names.Length - 1]; sepName[0] = ","; string[] nameTags = name.Split(sepName, StringSplitOptions.RemoveEmptyEntries); for (int j = 1; j < nameTags.Length - 1; j++) { List<string> tagList; tagList = nowLog.Tags.ToList(); if (nowLog.Tags == null || !nowLog.Tags.Contains(nameTags[j])) { tagList.Add(nameTags[j]); } nowLog.Tags = tagList.ToArray(); } if (nowLog.Tags != null && nowLog.Tags.Length > 0) ptag.allTags = nowLog.Tags.ToList(); //char[] sep = { '/', ' ', ':'}; char[] sep = { '/', ' ', ':', '-' }; // modified by Gengdai, 2011-11-9 string nowtime = DateTime.Now.ToString(); nowLog.CreateTimeStamp = nowtime.Split(sep, StringSplitOptions.RemoveEmptyEntries); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(filename[i]); int[] pidList = bmp.PropertyIdList; int index = Array.IndexOf(pidList, 0x9003); if (index == -1) { DateTime WriteTimeStamp = File.GetLastWriteTime(filename[i]); if (DateTime.Compare(WriteTimeStamp, PhotoLog.String2DateTime(nowLog.CreateTimeStamp)) < 0) { nowLog.CapturedTimeStamp = (WriteTimeStamp.ToString()).Split(sep, StringSplitOptions.RemoveEmptyEntries); } else { nowLog.CapturedTimeStamp = nowLog.CreateTimeStamp; } } else { System.Drawing.Imaging.PropertyItem pi = bmp.PropertyItems[index]; string cts = Encoding.ASCII.GetString(pi.Value, 0, 19); nowLog.CapturedTimeStamp = cts.Split(sep, StringSplitOptions.RemoveEmptyEntries); } //nowLog.Tags = lastPhoto.Tag.ToArray(); bmp.Dispose(); // nowLog.Feature, nolLog.Variance はテクスチャをロードしてから計算する 从纹理计算 // 在图片上标记时间(拍照和制作时间) ptag.CapturedDate = PhotoLog.String2DateTime(nowLog.CapturedTimeStamp); ptag.CreatedDate = PhotoLog.String2DateTime(nowLog.CreateTimeStamp); //calculate color feature color = Photo.CalcFeature(t); List<string> f = new List<string>(); for (int j = 0, len = color.feature_.Length; j < len; ++j) { f.Add(color.feature_[j].X.ToString()); f.Add(color.feature_[j].Y.ToString()); f.Add(color.feature_[j].Z.ToString()); } nowLog.Feature = f.ToArray(); nowLog.Variance = color.variance_.ToString(); photoLog[nowLog.FilePath] = nowLog; } //problem: two images cannot have the same filename newP = new Photo(i, filename[i], new Vector2( (float)(random_.NextDouble() * SystemParameter.ClientWidth), (float)(random_.NextDouble() * SystemParameter.ClientHeight)), (float)(random_.NextDouble() * 0.1) + 0.25f, 0f, ptag, t, color); photos.Add(newP); // people tag: added by Gengdai if (peopleTags.ContainsKey(filename[i])) { var p = peopleTags[filename[i]]; newP.PTags = p; // add tags directly foreach (PeopleTag pt in p.pTags) { newP.ptag.allTags.Add(pt.People); } } //photos[photos.Count - 1].SetTexture(t); newP.Center = new Vector2(t.Width, t.Height) * 0.5f; newP.BoundingBox = new BoundingBox2D(newP.Position - newP.Center * newP.Scale - Vector2.One * (float)ResourceManager.MAR, newP.Position + newP.Center * newP.Scale + Vector2.One * (float)ResourceManager.MAR, newP.Angle); progressBar.ProgressName(); //PeopleTags nowPeopleTags = new PeopleTags(filename[i]); //foreach (PeopleTags p in peopleTags) //{ // if (p.FileName == filename[i]) // { // photos[photos.Count-1].PTags = p; // // add tags directly // foreach (PeopleTag pt in p.pTags) // { // photos[photos.Count - 1].Tag.Add(pt.People); // } // break; // } //} } progressBar.End(); //colorTable.insert(newColors); }