2013年12月6日金曜日

コーディングを支える技術 成り立ちから学ぶプログラミング作法

コーディングを支える技術 成り立ちから学ぶプログラミング作法 ※この記事はCA14 Advent Calendar 2013の6日目の記事です。


周りがみんな為になる本を紹介しててビビりました。
普通に漫画か僕が大好きなマジシャンのマジックの本を紹介しようと思ってましたが、急遽、方向修正。笑
教授に面白いよーと勧められて読んだ本が素敵だったので紹介します!




どんな本?


某未踏やってたころに一度お会いしたことのある西尾さんの本です。
プログラミングをする際に使用する言語というものは多々ありますよね。 COBOL, CLU, C, C++, C#, Java, JS, Objective-C, PL/I, Python, Ruby・・・など書ききれないです。
その中で過去のプログラミングにおいて、設計者は何を問題点と考え、新しい言語を作ったのか。その歴史、問題点、特徴などから現在普及している言語の特徴や未解決の問題などが紹介されています。
第1章:言語を深く効率的に学ぶには
第2章:プログラミング言語を俯瞰する
第3章:文法の誕生
第4章:処理の流れのコントロール
第5章:関数
第6章:エラー処理
第7章:名前とスコープ
第8章:型
第9章:コンテナと文字列
第10章:並行処理
第11章:オブジェクトとクラス
第12章:継承によるコードの再利用
目次は↑のようになっています。
まず、4章〜12章まではがっつりな内容となっています。 しかも、著者が話したいことをただ書くのではなく、実際のコードを見ながら過去の言語と比較していくので読者が置いていかれずに読み進めることができます。
つまり、どうやってコーディングするかとかではなく、概念を考察していく本です。

ぼくのツボ


if文がない時代をアセンブリ言語から攻めるとか、エラー処理の歴史とかとってもたのしい章がたくさんあるんですけど、この本の個人的なツボは・・・
それはッ、ところどころに散りばめられたコラムーッ!! コラムのタイトルだけ抜粋すると、
理解をするためにまずアウトプット
何を学べばよいかわからない理由
具体的な知識と抽象的な知識
噛み砕く
必要なところからかじる
おおまかにつかんで徐々に詳細化する
(一部除く)
などです。
理解
ある事を勉強して、何をもって「理解できた」といえるのか?
正しいかどうかをアウトプットして第三者に確認してもらう。
学び
ひと通り学ぶことは不可能な場合が多い。何をゴールにするにするのか? 最初から完璧なものをゴールにするのか?
歩き出してから、自分ができること、学ぶべきことが見えてくる。
この繰り返しが自分の力になる
具体的と抽象的
応用範囲の限定された具体的なハウツーはどんどん価値を失う。
抽象的な概念だけを学んでもだめ。
抽象的な概念を具体的な説明に落とせることが重要。
必要なところからかじる
例えば、本やドキュメントの内容のすべてが同程度に必要な訳ではない。
重要なところをつかむ必要がある。
重要なところを探す
目次を見て構成を理解する。小見出しや強調、図表などをチェックする。
自分の目的にあった重要なところを探す。
写経
明確な「やりたいこと」「調べたいもの」がなく「おおまかに読む」場合、情報が頭に入りにくい。 そこで、知識の足がかりを作るために教科書を写経する。知識のコピーを行ってから、その足がかりを元に勉強してく。
上記のことは、プログラミングだけではなく様々なことに当てはまるのではないかと思います。


以上、「コーディングを支える技術 成り立ちから学ぶプログラミング作法」の紹介でしたー! コラムばっかり書きましたが、普通に全部面白いので、良かったら買ってみてください!!ヽ(=´▽`=)ノ
さて、明日は、szk氏か・・・期待!w

2013年12月3日火曜日

XperiaZの電池の減りが急激に早くなったけど治った

最近(Android4.2.2にアップデートしてから?)、電池の減りが急に早くなったなーって思ってました。
思ってたどころじゃなくて、画面OFF状態でも10分で1%減るくらいでした。
は・や・す・ぎ!!

犯人は。。。

電池の使用料を見ると、「GooglePlay開発者サービス」50%ほど、「ディスプレイ」が20%ほど。
普通ディスプレイが一番電池使用量が多いと思うんです。
特に、XperiaZって画面OFF時は極端に通信料を減らしている(気がするんです←)。
ですので、「GooglePlay開発者サービス」がどう考えてもあやしい。

とりあえず、再起動してみた。

調べてみると他アプリと連携してる可能性もあるのでアンインストールしたくなくて・・・。とりあえず、再起動したろっと思い再起動したら、今度はスマホの電池が50%から70%ほどに上がった。

な・ぜ!!!

それからは数日経過してるけど、元に戻ったような気がします。



電池使用量も「GooglePlay開発者サービス」じゃなく、「ディスプレイ」がトップになってるし。

まぁ、いけるでしょう!w

2013年11月8日金曜日

Texでフォント埋め込み&Type3フォントを使用していた場合の対処

2点についてです。
①Texでフォント埋め込み
TeXでPDFにフォントを埋め込む(dvipdfmx)

上記のサイトを参考に、まず、dviファイルを作成し、コマンドプロンプトで
dvipdfmx -f dlbase14.map paper.dvi
を実行するとpdfが作成され、大抵のフォントが埋め込まれている。
AdobeReaderで開いて、[ファイル]→[プロパティ]→[フォントタブ]で確認できる。
ほぼ全部に「埋め込みサブセット」と書かれていればOK


②Type3フォントの対処
上記の手順を行ってもType3フォントは埋め込まれない。
埋め込まれていないフォントは消さないと行けない。

pdfファイルにフォントが埋め込まれていない場合の対策

上記のサイトを参考にmacのプレビューからpdfを開き、pdfで書き出したらType3フォントが消えてる。
なんかこれずるく感じるけど、まいっかw

2013年10月6日日曜日

railsのコントローラでJSON返したいのに関連モデルが取得出来ない・・・

という問題で結構ハマりました。
が、検索したら普通にでてきました。笑

Railsにて関連モデルをまとめてJSONで出力する場合

だって、findとかでincludeやjoinしたものをそのままrenderするだけで、普通にJSON返せると思うじゃないですか!!
なんでですか!!

ちなみに、rails3.以降はjbuilderというテンプレートエンジンがあってこんなことしなくてもいいらしいですね。
http://railscasts.com/episodes/320-jbuilder?language=ja&view=asciicast




2013年9月24日火曜日

iPhone5S,5Cに搭載された指紋認証のすごいところ

iPhone5S,5Cが発売されて数日経過しましたねー。
やっぱり話題になっているのは指紋認証ではないでしょうか?


今回は、iPhoneの指紋認証は何がすごいのか、何故こんなにもてはやされるのかを書いていきたいと思います。


認証方法が変わったから?
指紋認証すごいですよね。
今までの認証のイメージだと、指を置いてスライドさせるイメージ。
(下のはThinkPadですが、ガラケーの頃からこんなイメージでしたよね??)

しかし、iPhoneはじーっとしとくだけになってますもんね。

昔、指紋認証付きのガラケーを使っていましたが、今考えると指をスライドさせるの面倒でしたよね。。。

やっぱり認証技術の進歩とともに、読み取り方法が変わってきて使いやすくなったので採用されたのかなと思います。


でも、ほんとに凄い所は別にある!
これってできそうじゃないですか?日本のガラケーでも、アンドロイドでも。
僕がこれに関して魅力的だなと感じていることは「知的システム」であり「知的な人工物」であるということです。

知的システム?知的な人工物?
知的システムや知的な人工物に関しては同志社大学の三木先生が研究されています。
簡単に言うと外部環境をSenseし、そのデータを元にJudgeし、何かactする。
Sense,Judge,Actの3つの要素を含むものです。
以下のリンクが参考になります。
http://mikilab.doshisha.ac.jp/dia/research/intellectualization01/intelligent/intelligent_top.html

例えば、自動ドアですね。
ドアを通りたい人が前に来るとセンサが反応し、人が来たと判断して、ドアを開ける。
もし、ドアにスイッチが付いていれば知的ではありません。

他には自動で水がでる蛇口ですね。(公衆トイレなどで見かけるやつです)
人が手を洗おうと蛇口に手を近づけると、センサが反応し、手が近くにあると判断し、水を出す。

これらは人が意識せずに、人以外のモノ(コンピュータなど)が判断し、動作する。
立派な知的な人工物です。
人の効率の向上や不必要な動作や判断を避けることができます。


iPhoneと知的人工物
iPhoneの指紋認証のすごいところに話を戻します。
この認証は知的かというと認証なので知的なのですが、いいところはホームボタンに認証機能があるということです。

ユーザは画面をつけるためにホームボタンを押し、その際に認証も行う。
これまでは、ユーザからすると画面をつけることと認証は全く違う動作でした。
しかし、iPhoneは自動で認証までユーザに意識をさせずに行うのです。
不必要な動作を避け、効率を向上する。
これは立派な知的なシステムだと思います。

もし、ホームボタンではなく、まったく違う位置に認証機能があれば、ダメな機能だったでしょう。
アンドロイドで指紋認証を載せたとしても、ホームボタンなどのユーザが普段行う動作のどこかに潜りこませないと良いシステムとは言えないでしょう。

iPhoneの指紋認証はこの知的システムだからすごい
と思います。


知的にデザインを行うことについて
人は効率を求めます。無駄なことや面倒なことはしたくありません。
無駄なことや面倒なことを避けるために、お金を使います。
身の回りでもそうでしょう。
知的にデザインを行うことは、多くの人に使ってもらうためのとても大切な要素だと思います。(お金も儲かるかも・・・w)

知的なシステムを考えると結構簡単に(アイデアのしょぼさは問わず←)考えられます。
今回のiPhoneの指紋認証の発展を考えれば、PCのキーボードに手を置いた瞬間に認証されればいいでしょう。(そもそも椅子に座った段階でお尻で認証する研究もあった気がする。)
PC以外に考えると、例えば、ドアノブを回すときに認証を行う。認証できなかったら鍵を開けないでおくなど。


これかも、「知的」というキーワードでアイデアを探し続けていこうと思います。





下の本はブログ中にも書きました、三木先生の本です。


知的システム工学 (情報工学テキストシリーズ 2)
三木 光範
共立出版
売り上げランキング: 1,050,626

2013年9月12日木曜日

NFCとiBeaconを比較している記事に対して1つ言いたいことがある

iOS7の隠れキラーコンテンツとなる近距離無線通信「iBeacon」とは? 

iPhone5S, 5Cの発売に関してのこの記事についてです。
◆NFCよりも優れるBeacon 近距離無線通信技術としてNFCと競合するBeaconですが、様々な点でBeaconが優れています。
と、書かれていますが、本当に?と思っています。

トリガーが違うよ!

NFC

タッチ(かざす)ことがトリガー

iBeacon

通りがかる(近くによる)ことがトリガー


この2つの違いは大きいと思います。
NFCはユーザにとって明確なトリガー、iBeaconはユーザにとって不明確な(自然な)トリガーです。

必ずしもiBeaconがいいのではない!(ツン)

例えば、自動販売機でジュースをスマホで決済することを想像してください。

この場合、僕は、ボタンを押して自動で決済してくれるiBeaconよりも従来のNFCが優れていると思います。
なぜなら、購入者は本当に購入の意図があったかが確認できるからです。
(子供が勝手に違うものを選んだとかではなく)

iBeaconでも確認画面をつければいいじゃないか?って思う人もいるかもしれません。
でも、それってiBeaconの良さが無くね???NFCでよくね???と思います。


長所もあるよ!(デレ)

もちろんiBeaconにも長所はあります。
屋内位置推定が一番の長所かなと。
ユーザの行動を分析し自動で情報を受信できる。
現在はポスターにNFCを埋め込む形式の広告があります。

僕は「広告=ユーザに無理やり見せようとするもの」という考え方をしています。
CMやWEBだってそうです。
その中でユーザにとって有益な広告があれば、ユーザは反応するのです。

ポスターに埋め込んだNFCに意図的にタッチして情報を取りに行くよりかは、
ポスターの前を通りがかるとポスターが語りかけてくるほうがいいですよね!

だから、こういうものに対してはNFCよりもiBeaconのほうが強いのではないでしょうか。


まとめ

つまり、NFCもiBeaconも全く利用方法や応用範囲が違うのではないかと思います。
そして、Googleさん、Appleさん、
NFCもiBeaconもどっちも使えるようにしてしまえばいいじゃないw




Android NFCプログラミング完全ガイド (Smart Mobile Developer) オレンジタグス 業務用 FeliCa フェリカ ラベルシール 45x35 非接触 ICタグ FeliCa Lite , TL-L4
オレンジタグス 業務用 NFC Forum Type2 Tag 非接触ICカード Mifare Ultralight , TU-C1
株式会社オレンジタグス
売り上げランキング: 64,702




2013年7月26日金曜日

SublimeText2とGoogle日本語入力を使用して、日本語コメントを入力してENTER押すと消える問題(長っ


タイトルどおりです。
問題はどういうことかというと、日本語コメント入力して、



ENTER押すと・・・・




消えた・・・\(^o^)/

おそらくはコメント行を自動で補完してくれる優しさが日本語に上書きされるのが問題だと思われます。

ググったらやさしい方がGitHubに回避方法を公開していました。
http://koizuss.hatenablog.com/entry/2013/01/14/112350
https://gist.github.com/koizuss/4521934
お、Default.sublime-keymapの36行から3つ分をコメントすればいいみたい。
やっほい!

Default.sublime-keymapはSublimeを立ち上げて、
 Preference > Browse Packages...
から、
/Packages/DocBlockr/Default.sublime-keymap
で開ける。


・・・
・・・・・・
直らない・・・・

結局設定を読んでいるともう一箇所コメントしないといけないことに気づく。
コメントアウトしたら上記の問題が回避できた。

結局、設定はこのようになりました↓
合計4箇所コメントアウトしてます。

[
  // open a docblock with enter
  { "keys": ["enter"], "command": "jsdocs",
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,                    "match_all": true },
      { "key": "selection_empty",       "operator": "equal",          "operand": true,                    "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,                   "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*(\\/\\*|###)\\*$", "match_all": true }
    ]
  },
  // open a docblock with keypad enter
  { "keys": ["keypad_enter"], "command": "jsdocs",
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,                    "match_all": true },
      { "key": "selection_empty",       "operator": "equal",          "operand": true,                    "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,                   "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*(\\/\\*|###)\\*$", "match_all": true }
    ]
  },
  // open a docblock with tab
  { "keys": ["tab"], "command": "jsdocs",
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,                    "match_all": true },
      { "key": "selection_empty",       "operator": "equal",          "operand": true,                    "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,                   "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*(\\/\\*|###)\\*$", "match_all": true }
    ]
  },

  /**
   * Google日本語入力を使っている場合のENTERで入力内容が消える問題に対応
   * ↓をコメントアウトすることでとりあえず入力できるようになる
   * (Docコメント内で改行しても整形されなくなる)
   */

  // // extend a docblock by adding an asterisk at the start
  // { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n${TM_CURRENT_LINE/^\\s*(\\*\\s*).*$/$1/}"},
  //   "context": [
  //     { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,              "match_all": true },
  //     { "key": "selector",              "operator": "equal",          "operand": "comment.block",   "match_all": true },
  //     { "key": "auto_complete_visible", "operator": "equal",          "operand": false,             "match_all": true },
  //     { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\*\\s*\\S", "match_all": true }
  //   ]
  // },
  // // extend a docblock by adding an asterisk at the start
  // { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n${TM_CURRENT_LINE/^\\s*(\\*\\s*).*$/$1/}"},
  //   "context": [
  //     { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,            "match_all": true },
  //     { "key": "selector",              "operator": "equal",          "operand": "comment.block", "match_all": true },
  //     { "key": "auto_complete_visible", "operator": "equal",          "operand": false,           "match_all": true },
  //     { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\*",      "match_all": true }
  //   ]
  // },
  // // extend a docblock with keypad enter by adding an asterisk at the start
  // { "keys": ["keypad_enter"], "command": "insert_snippet", "args": {"contents": "\n${TM_CURRENT_LINE/^\\s*(\\*\\s*).*$/$1/}"},
  //   "context": [
  //     { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,            "match_all": true },
  //     { "key": "selector",              "operator": "equal",          "operand": "comment.block", "match_all": true },
  //     { "key": "auto_complete_visible", "operator": "equal",          "operand": false,           "match_all": true },
  //     { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\*",      "match_all": true }
  //   ]
  // },

  // trim the automatically added whitespace
  { "keys": ["enter"], "command": "jsdocs_trim_auto_whitespace",
    "context": [
      { "key": "setting.auto_indent",                "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "selector",                           "operator": "equal",          "operand": "comment.block", "match_all": true },
      { "key": "auto_complete_visible",              "operator": "equal",          "operand": false,           "match_all": true },
      { "key": "setting.trim_automatic_white_space", "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "preceding_text",                     "operator": "regex_contains", "operand": "^\\s*\\*\\s*$", "match_all": true },
      { "key": "following_text",                     "operator": "regex_contains", "operand": "^\\s*$",        "match_all": true }
    ]
  },
  // trim the automatically added whitespace
  { "keys": ["keypad_enter"], "command": "jsdocs_trim_auto_whitespace",
    "context": [
      { "key": "setting.auto_indent",                "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "selector",                           "operator": "equal",          "operand": "comment.block", "match_all": true },
      { "key": "auto_complete_visible",              "operator": "equal",          "operand": false,           "match_all": true },
      { "key": "setting.trim_automatic_white_space", "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "preceding_text",                     "operator": "regex_contains", "operand": "^\\s*\\*\\s*$", "match_all": true },
      { "key": "following_text",                     "operator": "regex_contains", "operand": "^\\s*$",        "match_all": true }
    ]
  },
  // extend line comments (// and #)
  /**
   * ここでENTERキーを押してもコメントの行が自動補完されないようにする。
   */
//  { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n${TM_CURRENT_LINE/^\\s*((?:#|\\/\\/)\\s*).*/$1/}"},
//    "context": [
//      { "key": "setting.auto_indent",                "operator": "equal",          "operand": true,              "match_all": true },
//      { "key": "setting.jsdocs_extend_double_slash", "operator": "equal",          "operand": true,              "match_all": true },
//      { "key": "selector",                           "operator": "equal",          "operand": "comment.line",    "match_all": true },
//      { "key": "auto_complete_visible",              "operator": "equal",          "operand": false,             "match_all": true },
//      { "key": "preceding_text",                     "operator": "regex_contains", "operand": "^\\s*(\\/\\/|#)", "match_all": true }
//    ]
//  },
  // extend line comments (// #) with keypad enter
  { "keys": ["keypad_enter"], "command": "insert_snippet", "args": {"contents": "\n${TM_CURRENT_LINE/^\\s*((?:#|\\/\\/)\\s*).*$/$1/}"},
    "context": [
      { "key": "setting.auto_indent",                "operator": "equal",          "operand": true,           "match_all": true },
      { "key": "setting.jsdocs_extend_double_slash", "operator": "equal",          "operand": true,           "match_all": true },
      { "key": "selector",                           "operator": "equal",          "operand": "comment.line", "match_all": true },
      { "key": "auto_complete_visible",              "operator": "equal",          "operand": false,          "match_all": true },
      { "key": "preceding_text",                     "operator": "regex_contains", "operand": "^\\s*\\/\\/",  "match_all": true }
    ]
  },
  // close a block comment (/*  */)
  { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n$0\n */"},
    "context": [
      { "key": "selection_empty", "operator": "equal",          "operand": true,           "match_all": true },
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s*\\/\\*$", "match_all": true }
    ]
  },
  // close a block comment (/*  */)
  { "keys": ["keypad_enter"], "command": "insert_snippet", "args": {"contents": "\n$0\n */"},
    "context": [
      { "key": "selection_empty", "operator": "equal",          "operand": true,           "match_all": true },
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s*\\/\\*$", "match_all": true }
    ]
  },
  { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n$0\n "}, "context":
    [
      { "key": "selection_empty", "operator": "equal",          "operand": true,           "match_all": true},
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s*\\/\\*$", "match_all": true},
      { "key": "following_text",  "operator": "regex_contains", "operand": "^\\*\\/\\s*$", "match_all": true}
    ]
  },
  { "keys": ["keypad_enter"], "command": "insert_snippet", "args": {"contents": "\n$0\n "}, "context":
    [
      { "key": "selection_empty", "operator": "equal",          "operand": true,           "match_all": true},
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s*\\/\\*$", "match_all": true},
      { "key": "following_text",  "operator": "regex_contains", "operand": "^\\*\\/$",     "match_all": true}
    ]
  },
  // De-indent at the end of a comment block
  { "keys": ["enter"], "command": "jsdocs_deindent",
    "context": [
      { "key": "selection_empty", "operator": "equal",          "operand": true,        "match_all": true },
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s+\\*/", "match_all": true }
    ]
  },
  // de-indent at the end of a comment block with keypad-enter
  { "keys": ["keypad_enter"], "command": "jsdocs_deindent",
    "context": [
      { "key": "selection_empty", "operator": "equal",          "operand": true,        "match_all": true },
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s+\\*/", "match_all": true }
    ]
  },
  // Open an inline docblock (/** */)
  { "keys": ["shift+enter"], "command": "jsdocs", "args": {"inline": true},
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,              "match_all": true },
      { "key": "selection_empty",       "operator": "equal",          "operand": true,              "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,             "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\/\\*{2}$", "match_all": true }
    ]
  },
  // Open an inline docblock
  { "keys": ["shift+keypad_enter"], "command": "jsdocs", "args": {"inline": true},
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,              "match_all": true },
      { "key": "selection_empty",       "operator": "equal",          "operand": true,              "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,             "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\/\\*{2}$", "match_all": true }
    ]
  },
  // show the autocomplete
  { "keys": ["@"], "command": "run_macro_file", "args": {"file": "Packages/DocBlockr/jsdocs-auto-complete.sublime-macro"},
    "context": [
      { "key": "setting.auto_complete", "operator": "equal",    "operand": true,                           "match_all": true },
      { "key": "selection_empty", "operator": "equal",          "operand": true,                           "match_all": true },
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s*(?:\\/\\*|###)?\\*\\s*$", "match_all": true },
      { "key": "selector",        "operator": "equal",          "operand": "comment.block",                "match_all": true }
    ]
  },
  // show the autocomplete in a coffee doc block
  { "keys": ["@"], "command": "run_macro_file", "args": {"file": "Packages/DocBlockr/jsdocs-auto-complete.sublime-macro"},
    "context": [
      { "key": "selection_empty", "operator": "equal",          "operand": true,                              "match_all": true },
      { "key": "preceding_text",  "operator": "regex_contains", "operand": "^\\s*#\\s*$",                     "match_all": true },
      { "key": "selector",        "operator": "equal",          "operand": "comment.line.number-sign.coffee", "match_all": true }
    ]
  },
  // indent to align with the previous line
  { "keys": ["tab"], "command": "jsdocs_indent",
    "context": [
      { "key": "setting.jsdocs_deep_indent", "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "setting.auto_indent",        "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "selection_empty",            "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "preceding_text",             "operator": "regex_contains", "operand": "^\\s*\\*\\s*$", "match_all": true },
      { "key": "selector",                   "operator": "equal",          "operand": "comment.block", "match_all": true }
    ]
  },
  // decorate a double-slash comment
  { "keys": ["ctrl+enter"], "command": "jsdocs_decorate",
    "context": [
      { "key": "selector", "operator": "equal", "operand": "comment.line.double-slash"}
    ]
  },
  // decorate a double-slash comment
  { "keys": ["ctrl+keypad_enter"], "command": "jsdocs_decorate",
    "context": [
      { "key": "selector", "operator": "equal", "operand": "comment.line.double-slash"}
    ]
  },
  // join lines inside a comment block, stripping the leading asterisk
  { "keys": ["ctrl+j"], "command": "jsdocs_join",
    "context": [
      { "key": "selector", "operator": "equal", "operand": "comment.block" }
    ]
  },
  // join lines in a line comment, stripping the leading // or #
  { "keys": ["ctrl+j"], "command": "jsdocs_join",
    "context": [
      { "key": "selector", "operator": "equal", "operand": "comment.line" }
    ]
  },
  // reparse a comment block's placeholders
  { "keys": ["ctrl+alt+tab"], "command": "jsdocs_reparse",
    "context": [
      { "key": "selector", "operator": "equal", "operand": "comment.block" }
    ]
  },
  { "keys": ["alt+q"], "command": "jsdocs_wrap_lines",
    "context": [
      { "key": "selector", "operator": "equal", "operand": "comment.block", "match_all": true }
    ]
  },

  // add line after, in a DocBlock
  { "keys": ["ctrl+enter"], "command": "run_macro_file", "args": {"file": "Packages/DocBlockr/Add DocBlockr Line.sublime-macro"},
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "selector",              "operator": "equal",          "operand": "comment.block", "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,           "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\*",      "match_all": true }
    ]
  },

  // add line before, in a DocBlock
  { "keys": ["ctrl+shift+enter"], "command": "run_macro_file", "args": {"file": "Packages/DocBlockr/Add DocBlockr Line Before.sublime-macro"},
    "context": [
      { "key": "setting.auto_indent",   "operator": "equal",          "operand": true,            "match_all": true },
      { "key": "selector",              "operator": "equal",          "operand": "comment.block", "match_all": true },
      { "key": "auto_complete_visible", "operator": "equal",          "operand": false,           "match_all": true },
      { "key": "preceding_text",        "operator": "regex_contains", "operand": "^\\s*\\*",      "match_all": true }
    ]
  }
]


2013年1月3日木曜日

Android端末でGAEブロブストア上の画像の取得

Basic認証とストレージに画像保存とイメージビューに画像を保存まで。

package com.taku.nfchome;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import extra.UrlManager;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

public class ImageActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.image_activity);
  final ImageView iv = (ImageView)findViewById(R.id.imageView);
  
  new AsyncTask() {
   @Override
   protected Bitmap doInBackground(String... params) {
    Bitmap bmp = null;
    try{
     DefaultHttpClient httpClient = new DefaultHttpClient();
     String url = new String(params[0]);
     HttpGet request = new HttpGet(url);
     // 認証するUserとPW
     UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("「ユーザ名」","「パスワード」");
     AuthScope scope = new AuthScope(request.getURI().getHost(), request.getURI().getPort());
     httpClient.getCredentialsProvider().setCredentials(scope, credentials);
     
     HttpResponse httpResponse = httpClient.execute(request);
        
        // ステータスコードを取得
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        
        HttpGet httpRequest = new HttpGet(url); 
        
        //ーーー画像生成処理ーーー
        HttpResponse response = (HttpResponse) httpClient.execute(httpRequest); 
        HttpEntity entity1 = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity1);
        InputStream is = bufHttpEntity.getContent();
        bmp = getBitmap(is);
        // リソースを解放
        entity1.consumeContent();
        // クライアントを終了させる
        httpClient.getConnectionManager().shutdown();
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
    } catch (ClientProtocolException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
    
    return bmp;
   }
   @Override
   protected void onPostExecute(Bitmap bmp) {
    Log.v("image", "ok");
    iv.setImageBitmap(bmp);
    saveBitmapToSd(bmp);
    
   }
   
  }.execute("「URL」");
 }
 /**
  * 画像に変換
  * @param is
  * @return
  */
 public static Bitmap getBitmap(InputStream is) {
  Bitmap bmp = null;
  byte[] line = new byte[1024];
  int byteSize = 0;
  try {
   // バイト単位での読込
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   while ((byteSize = is.read(line)) > 0) {
    out.write(line, 0, byteSize);
   }
   // オプション
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inSampleSize = 0;
   
   byte[] byteArray = out.toByteArray();
   Log.v("image", "byteArray.length = " + byteArray.length);
   bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);
   is.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return bmp;
 }
 /**
  * 画像をストレージへ保存
  * @param mBitmap
  */
 public void saveBitmapToSd(Bitmap mBitmap) {
  try {
  // sdcardフォルダを指定
  File root = Environment.getExternalStorageDirectory();

  // 日付でファイル名を作成 
  Date mDate = new Date();
  SimpleDateFormat fileName = new SimpleDateFormat("yyyyMMdd_HHmmss");

  // 保存処理開始
  FileOutputStream fos = null;
  fos = new FileOutputStream(new File(root, fileName.format(mDate) + ".jpg"));
  Log.v("image", (new File(root, fileName.format(mDate) + ".jpg").toString()));
  // jpegで保存
  mBitmap.compress(CompressFormat.JPEG, 100, fos);
  // 保存処理終了
  fos.close();
  } catch (Exception e) {
   Log.e("Error", "" + e.toString());
  }
 }
}