1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| <?php/** * 圖片相似度比較 * * @version $Id: ImageHash.php 4429 2012-04-17 13:20:31Z jax $ * @author jax.hu * * <code> * //Sample_1 * $aHash = ImageHash::hashImageFile('wsz.11.jpg'); * $bHash = ImageHash::hashImageFile('wsz.12.jpg'); * var_dump(ImageHash::isHashSimilar($aHash, $bHash)); * * //Sample_2 * var_dump(ImageHash::isImageFileSimilar('wsz.11.jpg', 'wsz.12.jpg')); * </code> */class ImageHash { /**取樣倍率 1~10 * @access public * @staticvar int * */ public static $rate = 2; /**相似度允許值 0~64 * @access public * @staticvar int * */ public static $similarity = 80; /**圖片類型對應的開啟函數 * @access private * @staticvar string * */ private static $_createFunc = array( IMAGETYPE_GIF =>'imageCreateFromGIF', IMAGETYPE_JPEG =>'imageCreateFromJPEG', IMAGETYPE_PNG =>'imageCreateFromPNG', IMAGETYPE_BMP =>'imageCreateFromBMP', IMAGETYPE_WBMP =>'imageCreateFromWBMP', IMAGETYPE_XBM =>'imageCreateFromXBM', ); /**從檔案建立圖片 * @param string $filePath 檔案位址路徑 * @return resource 當成功開啟圖片則回傳圖片 resource ID,失敗則是 false * */ public static function createImage($filePath){ if(!file_exists($filePath)){ return false; } /*判斷檔案類型是否可以開啟*/ $type = exif_imagetype($filePath); if(!array_key_exists($type,self::$_createFunc)){ return false; } $func = self::$_createFunc[$type]; if(!function_exists($func)){ return false; } return $func($filePath); } /**hash 圖片 * @param resource $src 圖片 resource ID * @return string 圖片 hash 值,失敗則是 false * */ public static function hashImage($src){ if(!$src){ return false; } /*缩小圖片尺寸*/ $delta = 8 * self::$rate; $img = imageCreateTrueColor($delta,$delta); imageCopyResized($img,$src, 0,0,0,0, $delta,$delta,imagesX($src),imagesY($src)); /*計算圖片灰階值*/ $grayArray = array(); for ($y=0; $y<$delta; $y++){ for ($x=0; $x<$delta; $x++){ $rgb = imagecolorat($img,$x,$y); $col = imagecolorsforindex($img, $rgb); $gray = intval(($col['red']+$col['green']+$col['blue'])/3)& 0xFF; $grayArray[] = $gray; } } imagedestroy($img); /*計算所有像素的灰階平均值*/ $average = array_sum($grayArray)/count($grayArray); /*計算 hash 值*/ $hashStr = ''; foreach ($grayArray as $gray){ $hashStr .= ($gray>=$average) ? '1' : '0'; } return $hashStr; } /**hash 圖片檔案 * @param string $filePath 檔案位址路徑 * @return string 圖片 hash 值,失敗則是 false * */ public static function hashImageFile($filePath){ $src = self::createImage($filePath); $hashStr = self::hashImage($src); imagedestroy($src); return $hashStr; } /**比較兩個 hash 值,是不是相似 * @param string $aHash A圖片的 hash 值 * @param string $bHash B圖片的 hash 值 * @return bool 當圖片相似則回傳 true,否則是 false * */ public static function isHashSimilar($aHash, $bHash){ $aL = strlen($aHash); $bL = strlen($bHash); if ($aL !== $bL){ return false; } /*計算容許落差的數量*/ $allowGap = $aL*(100-self::$similarity)/100; /*計算兩個 hash 值的漢明距離*/ $distance = 0; for($i=0; $i<$aL; $i++){ if ($aHash{$i} !== $bHash{$i}){ $distance++; } } return ($distance<=$allowGap) ? true : false; } /**比較兩個圖片檔案,是不是相似 * @param string $aHash A圖片的路徑 * @param string $bHash B圖片的路徑 * @return bool 當圖片相似則回傳 true,否則是 false * */ public static function isImageFileSimilar($aPath, $bPath){ $aHash = ImageHash::hashImageFile($aPath); $bHash = ImageHash::hashImageFile($bPath); return ImageHash::isHashSimilar($aHash, $bHash); }} |
2014年6月2日 星期一
[PHP] 簡易的圖片相似度比較
2013年10月6日 星期日
[Android] dp px 互換(dp px exchange)
單位
dp : Density-independent pixel,獨立於螢幕解析度密度的像素值,也就是說,當你設定 1dp 時,在 120 dpi 的螢幕上會是原本的 75 %,在 160 dpi 的螢幕上為 100%,在 240 dpi 的螢幕上為 150%,實際的像素值會依螢幕密度而改變。px : pixel ,一般所認知的像素值,指定多少就是多少,不會因螢幕密度不同而改變。所以 100 * 100 px 的方形在 320 x 480 螢幕的手機上看起來會比 1280 x 720 的螢幕上看起來大很多,這個方形實際上尺寸並沒有改變,改變的是螢幕解析度,放在一個較小的空間看起來比較大,放在一個較大的空間,看起來就會比較小。
這裡有圖示解說,看圖比較容易懂。
轉換
如果單純只在 layout 上指定值,直接指定 dp 就好了,但如果要在程式碼中指定就必須要做轉換,否則都是 px ,我們必須把它轉為 dp 才能和其他在 layout 中指定的元件有一樣的比例。我自己是找到公式後寫成工具程式,直接叫用就好了,程式碼如下:
/**
* Covert dp to px
* @param dp
* @param context
* @return pixel
*/
public static float convertDpToPixel(float dp, Context context){
float px = dp * getDensity(context);
return px;
}
/**
* Covert px to dp
* @param px
* @param context
* @return dp
*/
public static float convertPixelToDp(float px, Context context){
float dp = px / getDensity(context);
return dp;
}
/**
* 取得螢幕密度
* 120dpi = 0.75
* 160dpi = 1 (default)
* 240dpi = 1.5
* @param context
* @return
*/
public static float getDensity(Context context){
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return metrics.density;
}
參考資料
http://developer.android.com/guide/practices/screens_support.htmlhttp://www.bbc.co.uk/gel/tablet/tablet-device-considerations/pixel-density
php 取得遠端網站的內容
取得遠端的網頁內容的函式名稱:
file_get_contents
用法:
$homepage=file_get_contents('http://www.kimo.com.tw');
echo $homepage
file_get_contents
用法:
$homepage=file_get_contents('http://www.kimo.com.tw');
echo $homepage
以PHP讀寫Excel檔案
以PHP讀寫Excel檔案
- Details
- Written by 小羅
http://phpexcel.codeplex.com/
用google找了一下,PHPExcel類別的功能非常強大,支援各版本的Excel檔案、CSV、HTML及PDF檔案的讀寫,有這方面的需求,改用這套工具可以省卻不少自行撰寫工具的時間,對使用者來說,Excel檔案的親和性比起CSV檔案來說,好太多了,所以決定使用Excel作為報表讀取檔案。
文章起頭的網址是PHPExcel的官網,從官網下載套件後,下載套件內的說明文件非常詳盡,花一二天就可以讀懂,使用上並不困難,只是要花時間熟悉使用方法。
PHPExcel的安裝
解壓縮下載的套件後,將Class目錄放到類別目錄下。
PHPExcel的使用
require Class/PHPExcel.php後,PHPExcel會自動載入需要的所有類別,不需作後續設定。
PHPExcel的設計哲學
物件分成三大區塊
1)Excel檔的操控--針對Excel2007版本的Excel進行操控,所有操控都在記憶體中進行,檔案的寫入及讀取由其他物件負責。
2)Excel檔的讀取--包含各版本Excel、PDF、CSV等
3)Excel檔的寫入--包含各版本Excel、PDF、CSV等
讀取Excel檔案的操件方法
1)由PHPExcel決定載入檔案的種類,如Excel版本,而不硬性設定。
1.$reader =
PHPExcel_IOFactory::load('excel檔');2)由PHPExcel決定載入檔案的種類,不硬性設定讀取的檔案種類,但將檔案設定為唯讀屬性。
1.$reader=
PHPExcel_IOFactory::createReaderForFile('excel檔'); 2.$reader->setReadDataOnly(true);
3.$excel= $reader->load('excel檔');讀取某一格的Excel內容
column由0開始
row由1開始
1.$sheet=$excel->getActiveSheet();
2.$sheet->getCell('A8') 3.->getValue();或
1.$sheet=$excel->getActiveSheet();
2.$sheet->getCellByColumnAndRow(2,8)
3.getValue();取得公式計算後的結果
1.$sheet=$excel->getActiveSheet();
2.$sheet->getCell('A8') 3.->getCalculatedValue();循序讀取cell的內容
01.<?php
02.$objReader =
PHPExcel_IOFactory::createReader('Excel2007'); 03.$objReader->setReadDataOnly(true);
04.$objPHPExcel = $objReader->load("test.xlsx"); 05.$objWorksheet = $objPHPExcel->getActiveSheet();
06.echo '<table>' . "\n";
07.foreach ($objWorksheet->getRowIterator()
as $row) { 08. echo '<tr>' . "\n";
09.$cellIterator = $row->getCellIterator();
10. $cellIterator->setIterateOnlyExistingCells(false); // This loops all cells, 11. // even if it is not set. 12. // By default, only cells 13. // that are set will be 14. // iterated. 15. foreach ($cellIterator as $cell) { 16. echo '<td>' . $cell->getValue() . '</td>'
. "\n";
17. } 18.echo '</tr>' . "\n";
19.}
20.echo '</table>' . "\n";
21.?>01.<?php
02.$objReader =
PHPExcel_IOFactory::createReader('Excel2007'); 03.$objReader->setReadDataOnly(true);
04.$objPHPExcel = $objReader->load("test.xlsx"); 05.$objWorksheet = $objPHPExcel->getActiveSheet();
06.$highestRow = $objWorksheet->getHighestRow();
// e.g. 10 07.$highestColumn = $objWorksheet->getHighestColumn();
// e.g 'F' 08.$highestColumnIndex =
PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5 09.echo '<table>' . "\n";
10.for
($row =
1; $row <= $highestRow; ++$row) { 11. echo '<tr>' . "\n";
12.for
($col =
0; $col <= $highestColumnIndex; ++$col) { 13. echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n"; 14. } 15.echo '</tr>' . "\n";
16.}
17.echo '</table>' . "\n";
18.?>設定使用第幾個活頁簿(index從0開始)
1.setActiveSheetIndex(0);使用流程
1)讀取Excel檔案或創建Excel檔案
2)解析並操控Excel檔案
3)將記憶體中的Excel轉換成其他的型態
訂閱:
文章 (Atom)