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
            }
      }
    }
}

2017年11月1日水曜日

アルバムから写真を取得して表示する (OpenCVでリサイズ)

ピッカーで選択した画像をリサイズして表示する

1. OpenCVの準備をする
2. Bridge header.hの修正 (以下を追加)
      #import "OpenCVWrapper.h"       // 追加
3. OpenCvWrapper.h の修正
     (作成する関数を記載する)
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface OpenCvWrapper : NSObject
// funciton to get opencv version
+(NSString * ) openCVVersionString;
+(UIImage * ) openCvResize:(UIImage * )image targetWidth:(int)width targetHeight:(int)height;

@end

4. C++コードを OpenCvWrapper.mm に記載する
#import <opencv2/opencv.hpp>        // 追加
// #import <opencv2/highgui.hpp>
#import <opencv2/imgcodecs/ios.h>   // UIImageToMatなどに必要(3.1.1以降はコレの模様)
#import "OpenCvWrapper.h"

@implementation OpenCvWrapper       // swiftからはこのクラス名でアクセス

// 追加関数 ()
+(NSString *) openCVVersionString
{
    return [NSString stringWithFormat: @"openCV Version %s", CV_VERSION];
}

// 引数x3を受けてUIImageを返す関数(param2,3はラベルをつけるらしい)
// 呼び出し側(Swift)はこんな感じに
// OpenCvWrapper.openCvResize(image, targetWidth: 320, targetHeight: 240)
+(UIImage*) openCvResize:(UIImage * )image targetWidth:(int)width targetHeight:(int)height
{
    // UIImage uiImag2
   
    NSLog(@"width:%d height:%d", width, height);
    if(width > height) {
        // 横画像
        NSLog(@"横画像");
    } else {
        // 縦画像
        NSLog(@"縦画像");
    }
   
    cv::Mat imageMat;
    UIImageToMat(image, imageMat);

    cv::Mat resizedMat(cv::Size(width, height), imageMat.type());
    cv::resize(imageMat, resizedMat, resizedMat.size(), cv::INTER_LINEAR);
   
    // cv::Mat resizedMat(cv::Size(10, 10), CV_8UC3, cv::Scalar(0,0,255));
   
    return MatToUIImage(resizedMat);
}

@end

5. ViewController.swiftに上記の関数を呼び出すコードを記載する

// 本アプリは以下の動作をします
// 1. ImageViewをタップするとpicker controllerが表示
// 2. picker controllerで写真を選択するとImgeViwに写真を表示

import UIKit

class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var imagePicker: UIImagePickerController!
    @IBOutlet weak var photoView: UIImageView!
    let tagPhotoView = 1
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        // viewにタグを設定(本筋ではないがImageViewをタッチした際にpicker controllerを表示させるため)
        photoView.tag = tagPhotoView
       
        // インスタンス生成
        imagePicker = UIImagePickerController()
       
       
        // デリゲート設定
        imagePicker.delegate = self
       
        // 画像の取得先はフォトライブラリとする(カメラにするとカメラを起動した撮影した画像を取得できるみたい)
        // この段階ではImagePickerは起動させません(エラーになるから...)
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        // self.presentViewController(imagePicker, animated: true, completion: nil)
    }

    override func viewDidAppear(animated: Bool) {
       
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    // ImagePickerControllerで画像を選択した際に呼び出されるdelegate (self指定の為)
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        // Add your functionality here
        //選択された画像を取得.
        let selectedImage: AnyObject?  = info[UIImagePickerControllerOriginalImage]
       
        //選択された画像を表示するViewContorllerにセットする.
        let image = (selectedImage as! UIImage)
        // photoView.image = (selectedImage as! UIImage)
       
        print("\(image.size.width)  x  \(image.size.height) : \(photoView.layer.bounds.width) x \(photoView.layer.bounds.height)")
       
        // リサイズ
        let resizedImage = OpenCvWrapper.openCvResize(image, targetWidth: 160, targetHeight: 160)
        // let resizedImage = OpenCvWrapper.openCvResize(image, targetWidth: Int32(photoView.layer.bounds.width), targetHeight: Int32(photoView.layer.bounds.height))
       
        // viewControllerに設定
        photoView.image = resizedImage
       
        // モーダルビューを閉じる
        self.dismissViewControllerAnimated(true, completion: nil)
    }
   
   
     // 画像選択がキャンセルされた時に呼ばれる.
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        // モーダルビューを閉じる
        self.dismissViewControllerAnimated(true, completion: nil)
       
    }
   
    // user interaction enable / multiple touchをチェックしておく
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesEnded(touches, withEvent: event)
        for touch: UITouch in touches {
            let tag = touch.view!.tag
            switch tag {
            case tagPhotoView:
                self.presentViewController(imagePicker, animated: true, completion: nil)
            default:
                break
            }
        }
    }
}

※ ImagePickerのサンプル で、取得した画像をリサイスするように修正したコードです
http://tantan2014.blogspot.jp/2017/10/uiimagepickercontroller.html

2017年10月30日月曜日

アルバムから写真を取得して表示する(UIImagePickerController)

UIImagePickerControllerを使って写真を取得してUIImageViewに表示します。
動作としては、
1. ImageViewをタップするとpicker controllerが表示
2. picker controllerで写真を選択するとImgeViewに写真を表示
となります。

1. info.plistに "Privacy - Photo Library Usage Description"を追加して、Valueにメッセージ("アルバムにアクセスします"とか)を記載します。
2. MainStoryBoardにImaveViewを配置しますし、ViewControllerにIBOutlet接続します。
        @IBOutlet weak var photoView: UIImageView! とか
3. ViewControllerクラスの継承元に以下を追加する
  UIImagePickerControllerDelegate,
   UINavigationControllerDelegate
  こんな感じになる↓
  class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate

4. クラスメンバに変数追加
      var imagePicker: UIImagePickerController!
    let tagPhotoView = 1
5. ViewDidLoadedに以下の処理を記載 (ViewDidLoadedでimagepickerControllerを起動させようとするとエラーが出るのでやってないというか)
     override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      
        // viewにタグを設定(本筋ではないがImageViewをタッチした際にpicker controllerを表示させるため)
        photoView.tag = tagPhotoView
      
        // インスタンス生成
        imagePicker = UIImagePickerController()
       
        // デリゲート設定
        imagePicker.delegate = self
      
        // 画像の取得先はフォトライブラリとする(カメラにするとカメラを起動した撮影した画像を取得できるみたい)
        // この段階ではImagePickerは起動させません
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        // self.presentViewController(imagePicker, animated: true, completion: nil)
    }

4. ViewControllerがタップされたらImagePicker controllerを起動するコード
    /*
        user interaction enable / multiple touchをチェックしておく
     */
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesEnded(touches, withEvent: event)
        for touch: UITouch in touches {
            let tag = touch.view!.tag
            switch tag {
            case tagPhotoView:
                self.presentViewController(imagePicker, animated: true, completion: nil)
            default:
                // self.presentViewController(imagePicker, animated: true, completion: nil)
                break
            }
        }
    }

5. imagePickerControllerで画面が選択されたら呼ばれる関数を記述
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        // Add your functionality here
        //選択された画像を取得.
        let selectedImage: AnyObject?  = info[UIImagePickerControllerOriginalImage]
       
        //選択された画像を表示するViewContorllerにセットする.
        photoView.image = (selectedImage as! UIImage)
       
        // モーダルビューを閉じる
        self.dismissViewControllerAnimated(true, completion: nil)

        // ステータスバーの非表示 (ただし、時計表示などは消えないみたい)
        // self.setNeedsStatusBarAppearanceUpdate()
    }
   
     // 画像選択がキャンセルされた時に呼ばれる.   
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {       
        // モーダルビューを閉じる
        self.dismissViewControllerAnimated(true, completion: nil)    
    }

とりあえず上記実装で意図した動作はするようです。


ソースコード全体
// 本アプリは以下の動作をします
// 1. ImageViewをタップするとpicker controllerが表示
// 2. picker controllerで写真を選択するとImgeViwに写真を表示

import UIKit

class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var imagePicker: UIImagePickerController!
    @IBOutlet weak var photoView: UIImageView!
    let tagPhotoView = 1
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        // viewにタグを設定(本筋ではないがImageViewをタッチした際にpicker controllerを表示させるため)
        photoView.tag = tagPhotoView
       
        // インスタンス生成
        imagePicker = UIImagePickerController()
       
       
        // デリゲート設定
        imagePicker.delegate = self
       
        // 画像の取得先はフォトライブラリとする(カメラにするとカメラを起動した撮影した画像を取得できるみたい)
        // この段階ではImagePickerは起動させません(エラーになるから...)
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }

    override func viewDidAppear(animated: Bool) {
       
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        // Add your functionality here
        //選択された画像を取得.
        let selectedImage: AnyObject?  = info[UIImagePickerControllerOriginalImage]
       
        //選択された画像を表示するViewContorllerにセットする.
        photoView.image = (selectedImage as! UIImage)
       
        // モーダルビューを閉じる
        self.dismissViewControllerAnimated(true, completion: nil)
    }
   
   
     // 画像選択がキャンセルされた時に呼ばれる.
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        // モーダルビューを閉じる
        self.dismissViewControllerAnimated(true, completion: nil)
       
    }
   
    // user interaction enable / multiple touchをチェックしておく
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesEnded(touches, withEvent: event)
        for touch: UITouch in touches {
            let tag = touch.view!.tag
            switch tag {
            case tagPhotoView:
                self.presentViewController(imagePicker, animated: true, completion: nil)
            default:
                break
            }
        }
    }
}

覚書...
ViewDidLoadedでimage picker controllerを起動させた時に表示されるエラーメッセージ
PhotoViewer01[27898:560863] Warning: Attempt to present <UIImagePickerController: 0x7fcd3300f600> on <PhotoViewer01.ViewController: 0x7fcd32622b10> whose view is not in the window hierarchy!