Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            savePath    = tbSavePath.Text.Trim();
            jsonVersion = tbJsonVersion.Text.Trim();
            shift       = Int32.Parse(tbShift.Text.Trim());
            subImageNum = Int32.Parse(this.tbSubImageNum.Text.Trim());
            basePath    = System.IO.Path.GetDirectoryName(tbPath.Text);
            if (Directory.Exists(savePath))
            {
                DeleteADirectory(savePath);
            }
            System.IO.Directory.CreateDirectory(savePath);

            //读取文件内容
            StreamReader sr = new StreamReader(tbPath.Text, System.Text.Encoding.Default);

            json = sr.ReadToEnd().TrimStart();
            sr.Close();

            JObject finaljObject = new JObject();
            JObject onlyAssets   = new JObject();
            //string json = "{\"orderId\":\"000001\",\"goodsId[0]\":\"001\",\"goodsId[3]\":\"003\",\"goodsId[10]\":\"new data\"}";
            JObject jsonObj = (JsonConvert.DeserializeObject(json) as JObject)["assets"] as JObject;

            finaljObject.Add(new JProperty("tags", (JsonConvert.DeserializeObject(json) as JObject)["tags"]));
            foreach (var p in jsonObj.Properties().ToArray())
            {
                string   id  = p.Name;
                OneAsset one = JsonConvert.DeserializeObject <OneAsset>(p.Value.ToString());
                if (one.state == 0)
                {
                    LogHelper.WriteLog(one.name + " 未查看,直接跳过截图");
                    continue;
                }

                #region 这里读取图片进行等分
                //等分数量
                //int subImageNum = Int32.Parse(this.tbSubImageNum.Text.Trim());
                //得到原图的宽高
                int subWidth, subHeight;
                //srcHeight = 2056;    ////获取原图宽、高  
                //srcWidth = 2456;
                subWidth  = one.size.width / subImageNum + shift;
                subHeight = one.size.height / subImageNum + shift;
                if (!crop(Path.Combine(basePath, HttpUtility.UrlDecode(one.name)), subWidth, subHeight, subImageNum))
                {
                    continue;
                }
                #endregion

                #region
                //所有缺陷框
                RTree.RTree <Region> tree = new RTree.RTree <Region>();
                if (one.state == 2)//这里需要操作素材打的框
                {
                    #region 这里写单个的区块的素材操作了
                    string oneJsonFile = Path.Combine(basePath, id + "-asset.json");
                    using (StreamReader srTemp = new StreamReader(oneJsonFile, System.Text.Encoding.Default))
                    {
                        OneAssetWithRegions oneAR = JsonConvert.DeserializeObject <OneAssetWithRegions>(srTemp.ReadToEnd().TrimStart());

                        #region 重头戏来了,这里处理完救赎
                        foreach (var f**k in oneAR.regions)
                        {
                            tree.Add(new RTree.Rectangle(f**k.boundingBox.left,
                                                         f**k.boundingBox.top,
                                                         f**k.boundingBox.left,
                                                         f**k.boundingBox.top,
                                                         0, 0), f**k);
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
                string oldName = one.name;
                #region 这里做一些逻辑判断
                for (int n = 1; n <= subImageNum * subImageNum; n++)
                {
                    one.id   = id + n;
                    one.size = new AssetsSize()
                    {
                        width = subWidth, height = subHeight
                    };                                                                    // 这里其实写不写无所谓,但追求完美还是写!!!!注意如果下次还要等分,这里还是写完整
                    one.name = Path.GetFileNameWithoutExtension(oldName) + "-" + n + Path.GetExtension(oldName);
                    one.path = "file:${path}" + one.name;

                    if (tree.Count > 0)
                    {
                        //判断所有的边框是否存在在裁剪后的边框内
                        F**k f**k    = cropRRectangles[n - 1];
                        var  objects = tree.Contains(f**k.rectangle);
                        if (objects.Count > 0)//有边框那么,改状态,还要生成本地json文件
                        {
                            one.state = 2;
                            OneAssetWithRegions fuckOne = new OneAssetWithRegions();
                            fuckOne.asset   = one;
                            fuckOne.version = jsonVersion;

                            foreach (var re in objects)
                            {
                                re.boundingBox = new BoundingBox()
                                {
                                    left   = re.boundingBox.left - f**k.point.X,
                                    top    = re.boundingBox.top - f**k.point.Y,
                                    width  = re.boundingBox.width,
                                    height = re.boundingBox.height
                                };
                                re.points.Clear();
                                re.points.Add(new AssetPoint()
                                {
                                    x = re.boundingBox.left, y = re.boundingBox.top
                                });
                                re.points.Add(new AssetPoint()
                                {
                                    x = re.boundingBox.left + re.boundingBox.width, y = re.boundingBox.top
                                });
                                re.points.Add(new AssetPoint()
                                {
                                    x = re.boundingBox.left + re.boundingBox.width, y = re.boundingBox.top + re.boundingBox.height
                                });
                                re.points.Add(new AssetPoint()
                                {
                                    x = re.boundingBox.left, y = re.boundingBox.top + re.boundingBox.height
                                });
                            }

                            fuckOne.regions = objects;
                            File.WriteAllText(Path.Combine(savePath, id + n + "-asset.json"), JsonConvert.SerializeObject(fuckOne));
                            onlyAssets.Add(new JProperty(one.id, JObject.Parse(JsonConvert.SerializeObject(one))));
                            LogHelper.WriteLog("ng " + HttpUtility.UrlDecode(one.name));
                        }
                        else//没有那么
                        {
                            one.state = 1;
                            if (cbSaveOk.Checked)
                            {
                                onlyAssets.Add(new JProperty(one.id, JObject.Parse(JsonConvert.SerializeObject(one))));
                            }
                            LogHelper.WriteLog("ok " + HttpUtility.UrlDecode(one.name));
                        }
                    }
                    //if (one.state == 1)//这里不用判断素材框, 直接草他个等分数的平方
                    //{

                    //}
                    //else


                    //#region 这里处理完了就可以知道 图片等分出来的区块的state,然后新建个-asset.json,然后再吧assets新增
                    ////one.state = 2;

                    //#endregion
                }
                #endregion
                //写入一个字符串
                int o = 0;
            }
            finaljObject.Add(new JProperty("assets", onlyAssets));
            string bb = finaljObject.ToString();
            File.WriteAllText(Path.Combine(savePath, "import.power-ai"), bb);

            MessageBox.Show("处理完成");


            //            ////////给原图分块
            //            for (int j = 0; j < subImageNum; j++)

            //            {
            //                for (int i = 0; i < subImageNum; i++)
            //                {
            //                    if (j < subImageNum - 1 && i < subImageNum - 1)
            //                    {
            //                        cv::Mat temImage(subHeight, subWidth, CV_8UC3, cv::Scalar(0,0,0));
            //            cv::Mat imageROI = src(cv::Rect(i * subWidth, j * subHeight, temImage.cols, temImage.rows));
            //            cv::addWeighted(temImage, 1.0, imageROI, 1.0, 0., temImage);
            //            subImages.push_back(temImage);
            //        }else{                
            //                cv::Mat temImage(srcHeight - (subImageNum - 1) * subHeight, srcWidth - (subImageNum - 1) * subWidth, CV_8UC3, cv::Scalar(0,0,0));
            //                cv::Mat imageROI = src(cv::Rect(i * subWidth, j * subHeight, temImage.cols, temImage.rows));
            //        cv::addWeighted(temImage, 1.0, imageROI, 1.0, 0., temImage);
            //                subImages.push_back(temImage);                
            //            }
            //}
            //std::cout<<subImages[0]<<std::endl;   //测试语句,打印出子图矩阵
        }
Esempio n. 2
0
        void AddElement(MeshElement element)
        {
            m_meshDict.Add(element.meshId, element);

            m_rtree.Add(element.bounding, element);
        }