2018年5月3日木曜日

VisualStudioCommunity2015でインストールが進まない

ASP.netのインストールでプログレスバーが進まない場合は、TiWorker.exeを落とすと解決しました。

1.タスクマネージャー
2.詳細
3.TiWorker.exeを右クリック
4.待機チェーンの分析
5.全部チェックしてプロセスの終了ボタン

上記操作でプログレスバーが先に進みます。

2018年4月8日日曜日

C#で正規表現

基本
. : 任意の1文字
+ : 直前の文字が文字が無いか1文字以上連続
* : 直前の文字が1文字以上連続
? : 直前の文字がないか、1文字あるか

^ : 行頭
$ : 行末

hoge[a-z]hoge : []内のアルファベットいずれか
hoge[^1-9]hoge : []内は数字以外

()はグループ化
(aaa|bbb) : aaaかbbbいずれか
hoge(eb)+ : hogeeb, hogeebeb, hogeebebeb など

\d : 数字

こちらのサイトがとても参考になります
http://hodade.com/seiki/page.php?chapter_3

一例ですが、以下からxxx-xxxxを抜き出したい場合 (xが数字)
123-4567 111111-6666 9990-999

こんな感じで
(\s|^)\d{3}-\d{4}\s
-行頭かスペースで始まり
- 数字3桁
- "-"を挟んで
- 数字4桁
- 空白で終わり
という意味になります


C#だと"Matches"を使うとマッチした内容がリストに追加されるので以下のようなコードが書けます

例: textbox_inの文字列をtextbox_regのパターンで検索する場合
---
using System.Windows.Forms;

using System.Text.RegularExpressions;

namespace RegEx01
{
    public partial class Form1 : Form
    {


        private void button_exe_Click(object sender, EventArgs e)

        {
            var text = textBox_in.Text;
            // var pattern = @"\s+";       // space
            var pattern = textBox_regPattern.Text;
            var regex = new Regex(pattern);
            textBox_out.Text = "";

            /*
            // 存在するかどうかを調べる
            var match = regex.IsMatch(text);

            textBox_out.Text = match.ToString();
            */


            // 検索結果を表示
            var matches = regex.Matches(text);
            foreach( var match in matches)
            {
                textBox_out.Text += match.ToString() + "\r\n";
            }

2018年2月24日土曜日

画像ファイルのノルム値を比較

- input側のtextboxにファイルをまとめてドラッグ&ドロップ
- "計算"ボタンでoutput側textboxにファイル名とノルム地をカンマ区切りで表示

using OpenCvSharp;

namespace PhotoDiffAnalyzer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 入力ファイルリスト
        List<string> sourceFileList = new List<string>();

        /// <summary>
        /// Ctrl-A対応
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_DiffFiles_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
                textBox_DiffFiles.SelectAll();
        }


        private void textBox_InputFiles_Enter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                e.Effect = DragDropEffects.Copy;

            }
            else
            {

                e.Effect = DragDropEffects.None;

            }
        }

        private void textBox_InputFiles_Drop(object sender, DragEventArgs e)
        {
            // 操作対象のリストをクリアする
            sourceFileList.Clear();
            textBox_InputFiles.Text = "";


            // ファイルリスト更新
            string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (string file in fileName)
            {
                if (File.Exists(file))
                {
                    string ext = Path.GetExtension(file).ToUpper();
                    if (ext == ".JPG" || ext == ".BMP" || ext == ".PNG")
                    {
                        // 入力ファイルリストに追加
                        sourceFileList.Add(file);

                        // 表示更新
                        string dispStr = Path.GetFileName(file);
                        textBox_InputFiles.Text += dispStr + "\r\n";
                    }
                }
            }
        }

        private void button_Exec_Click(object sender, EventArgs e)
        {
            String outStr = "";

            foreach(String File in sourceFileList)
            {
                Mat image = new Mat(File);

                image.Canny(120, 80);
                double norm = Cv2.Norm(image);

                outStr = Path.GetFileName(File) + "," + norm.ToString() + "\r\n";

                textBox_DiffFiles.Text += outStr;
            }
        }
    }
}

2018年2月18日日曜日

ファイルをまとめてドラッグ&ドロップ

1. ファイルをまとめてテキストボックスにドラッグ&ドロップ
2. テキストボックスに↑のファイルを表示

という動作をさせます。

1. FormにTextBoxを配置
2. TextBoxのプロパティのAllowDropをtrueにする
3. 上記TextBoxに "DragEnger" , "DragDrop"イベントを追加
4. 上記イベントを実装
        private void textBoxPhotoFile_Enter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            } else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        // 入力ファイルリスト
        List<string> sourceFileList = new List<string>();

        /// <summary>
        /// 操作対象の画像ファイルをドラッグ & ドロップした際にfileListとして登録する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxPhotoFileDrop(object sender, DragEventArgs e)
        {
            // 操作対象のリストをクリアする
            sourceFileList.Clear();
            textBox_InputFile.Text = "";

            // ファイルリスト更新
            string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (string file in fileName)
            {
                if (File.Exists(file))
                {
                    string ext = Path.GetExtension(file).ToUpper();
                    if(ext == ".JPG")
                    {
                        // 入力ファイルリストに追加
                        sourceFileList.Add(file);

                        // 表示更新
                        string dispStr = Path.GetFileName(file);
                        textBox_InputFile.Text += dispStr + "\r\n";
                    }
                }
            }
        }

2018年1月21日日曜日

OneDriveでgitを使ってみる

remote repository にgit公式を使用すると、無料だと公開設定になってしまうそうなので、OneDriveを remote repositoryとして使ってみます。
(Visual Studioにもgitが入っているようですが、よく解らないのでtortoise gitを使います)

準備 (以下をinstall)
- git
- tortoise Git
- tortoise Git日本語language pack
順にinstallしてからExplorer右クリックでtortoise Git->設定を選択して"日本語"を選択する

リポジトリの生成作業
ローカルリポジトリを作成 -> リモートリポジトリを作成 と、それぞれを個別にやっても問題ないようですが、以下の手順でやってみました。
commitはローカルリポジトリで行い、pushはOneDriveにあるリモートリポジトリのファイルにおこなわれるので、ネットにつないであればOneDriveに同期される筈です。

OneDriveにリモート側リポジトリを生成
1. リポジトリとなるフォルダを作る
2. 上記フォルダ内で右クリック -> Gitここにリポジトリを生成
3. "Bareを生成"をチェックしてOK

ローカルリポジトリの生成
1. VisualStudioで生成したフォルダに移動
 (slnのある場所の一つ上)
2. 右クリック -> Gitここにリポジトリを生成
 ("Bareを生成"をチェックしない)
3. 登録するファイルを選択して"Gitコミット"
 ("1"のフォルダを指定して右クリックした場合はを以下に記載)
4. フォルダ下のファイルが表示されるがチェックボックスが外れているので必要なファイルをチェックする
 この辺は不要
  - bin下
  - obj下
  - *.suo
5. メッセージを記載して"コミット"ボタン押下

リモート側リポジトリと同期
1. ローカル側のフォルダトップを右クリックして"Git同期"を選択
2. "リモート"を選択
3. "URL:" に OneDriveフォルダ(C:\Users\****\OneDrive\ドキュメント\git\hogehoge)を記載。この時、"リモート"に"origin"という記載がされる
4. "新規に追加/保存"ボタンを押下

2018年1月7日日曜日

C#でExifを読む

C#のformアプリでexifを読みます。
"rational"の記載のあるものについては XX/YY というような表記をするようで、WORD1/WORD2 のようにすればよいようです。
以下は参照ボタンでOpenFileDialogにてパスを指定取得し、ボタンを押下することで街頭パスのExif情報をTextBoxに表示する例となります。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Imaging;       // PropertyItems用

namespace ExifReader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        // ボタンを押下するとOpenFileDialogによりファイルパスを取得する
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_FileName.Text = openFileDialog1.FileName;
            }
        }

        // 表示ボタン押下でtextboxにExif情報を読んだ結果を表示する
        private void button2_Click(object sender, EventArgs e)
        {
            string filename = textBox_FileName.Text;
            Bitmap bmp = new Bitmap(filename);  // Exifのついたファイルを読み込む

            textBox_Output.Text = "";

            // 列挙してみる
            for (int i = 0; i < bmp.PropertyItems.Length; i++)
            {
                if (bmp.PropertyItems[i].Type == 2)
                {
                    string str = System.Text.Encoding.ASCII.GetString(bmp.PropertyItems[i].Value);
                    str = str.Trim(new char[] { '\0' });
                    textBox_Output.Text += string.Format("{0:X}:{1:d}:{2:s}\r\n",
                      bmp.PropertyItems[i].Id, bmp.PropertyItems[i].Type, str);
                }
                else {
                    textBox_Output.Text += string.Format("{0:X}:{1:d}:{2:d}\r\n",
                      bmp.PropertyItems[i].Id, bmp.PropertyItems[i].Type, bmp.PropertyItems[i].Len);
                }
            }

            // indexを使って直接取得してみる
            // ISOSpeedRatings
            int[] pils = bmp.PropertyIdList;

            int index = Array.IndexOf(pils, 0x8827);
            if(index > 0)
            {
                PropertyItem pi = bmp.PropertyItems[index];

                int iso = BitConverter.ToUInt16(pi.Value, 0);
                textBox_Output.Text += string.Format("iso: {0:d}\r\n", iso);        // iso: 100 と表示された
            }

            // https://msdn.microsoft.com/ja-jp/library/system.drawing.imaging.propertyitem.id(v=vs.110).aspx

            // FNumber
            index = Array.IndexOf(pils, 0x829d);
            if (index > 0)
            {
                PropertyItem pi = bmp.PropertyItems[index];

                // "rational"の解析は以下のようにする
                uint fn1 = BitConverter.ToUInt32(pi.Value, 0);
                uint fn2 = BitConverter.ToUInt32(pi.Value, 4);
                textBox_Output.Text += string.Format("Fnumber: {0:d}/{1:d}\r\n", fn1, fn2);        // Fnumber: 56/10
            }

            // ExposureTime
            index = Array.IndexOf(pils, 0x829a);
            if (index > 0)
            {
                PropertyItem pi = bmp.PropertyItems[index];
                uint exp1 = BitConverter.ToUInt32(pi.Value, 0);
                uint exp2 = BitConverter.ToUInt32(pi.Value, 4);
                textBox_Output.Text += string.Format("ExposureTime: {0:d}/{1:d}\r\n", exp1, exp2);        // exposuretime: 10/2000

            }
            // ExposureBiasValue
            index = Array.IndexOf(pils, 0x9204);
            if (index > 0)
            {
                PropertyItem pi = bmp.PropertyItems[index];
                int exp1 = BitConverter.ToInt32(pi.Value, 0);
                int exp2 = BitConverter.ToInt32(pi.Value, 4);
                textBox_Output.Text += string.Format("ExposureBiasValue: {0:d}/{1:d}\r\n", exp1, exp2); // ExposureBiasValue: 4/6
            }
      }
    }
}