- Oct 13 Fri 2017 11:24
自製Arduino 電路板
- Oct 13 Fri 2017 05:31
如何在Portable Python 2.7.6.1 安裝opencv模組
(1) 開啓下載網頁 https://pypi.python.org/pypi/opencv-python
(2) pip install opencv-python
(3) 將會自動 Import the opencv package & numpy
Installing collected packages: numpy, opencv-python
Found existing installation: numpy 1.8.1
DEPRECATION: Uninstalling a distutils installed project (numpy) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
Uninstalling numpy-1.8.1:
Successfully uninstalled numpy-1.8.1
Successfully installed numpy-1.13.3 opencv-python-3.3.0.10
(4)測試模組
c:\python> python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
3.3.0
>>> import numpy
>>> print numpy.__version__
1.13.3
>>>
- Oct 12 Thu 2017 10:23
weed detection
(1) Image Processing For Weed Detection
April 2017, Volume 5, Issue 4, ISSN 2349-4476 International Journal of Engineering Technology, Management and Applied Sciences Abstract: This paper introduces the system implementation of image processing technique for weed detection and removal. It involves simple edge detection technique using various filters such as Gaussian and Laplacian. It finally concludes with the feature extraction results that implement ORB algorithm. An RGB image is taken as a sample in order to demonstrate the difference between weed and the crop. This RGB image is further processed for detecting the weeds. We used Python 3.4.1 version for processing the sample image. After certain steps, we get an output where the weeds are separated from the crop that has been taken in the sample image. Keywords: Python, Image Processing, Featur
(2)A computer vision application to detect unwanted weed in early stage crops
Volume 4, 2016 WSEAS TRANSACTIONS on COMPUTER RESEARCH Abstract: This article shows an application for computer vision to detect unwanted weed in crops from one region with more agricultural impact in Colombia. We took images in different crops, wanting estimate population degree from arvenses species. An Image processing was developed to obtain regions of interest were finally processed through neural networks.
(3)WEED DETECTION USING IMAGE PROCESSING
2016 IRJET | Impact Factor value: 4.45 International Research Journal of Engineering and Technology (IRJET) Abstract - Agriculture is one of the origins of human sustenance in this world. Nowadays due to growing population we need the greater productive capability of the agriculture to meet the demands. In olden days, people used natural methods to increase the productivity, such as using the cow dung as a fertilizer in the fields. That resulted in an increase in the productivity enough to meet the requirements of the population. But later people thought of earning more profits by getting more outcome. So, there came a revolution called “Green Revolution”. After this period usage of deadly poisons as herbicides has increased to a drastic level. By doing so we got success in increasing the productivity but we have forgotten the damage done to the environment, which will raise a doubt in our sustenance on this beautiful earth. So, in this project, we have implemented some methods to reduce the usage of herbicides by spraying them only in the areas where weed is present. In this paper, we have implemented image processing using MATLAB to detect the weed areas in an image we took from the fields.
(4) A Survey on Weed Detection using Image Processing
2017- International Journal of Engineering Research & Technology (IJERT) Abstract: Agriculture is the origins of human sustenance in this world. The detection and classification of weeds are of most important technical and economical importance in the agricultural industry. In past days weed detection was done by employing some men, particularly for that intention. In olden days, weed detection was done by inspecting each and every place in the field. Then weeds were manually removed. Later with the improvement in the technology, people started using herbicides to take out the weeds. But to identify the weed still physical power was used in many parts of the world. Later there came few methods to discover the weeds without human intervention but due to lack of their accuracy, they were incapable to reach to the public. Then image processing was used for this purpose. In future it is planned to detect the weed in the crop by using image processing techniques. This paper focuses on survey of weed detection using image processing techniques.
(5) Weed Detection in Agriculture Using Image Processing
2017- International Journal of Advanced Research in Electrical, Electronics and Instrumentation Engineerin ABSTRACT: Nowadays everything has changed modern. But our basic needs has not changed and its demand is increased by the increase in population. The need for food has increased by every decades. Agriculture is the backbone of our country and our country is famous for all spices. But now our agricultural lands are devastated by modernization and in the remaining agricultural land, more use of herbicides make them poisonous. In the early days all foods are organic and those people were healthy. But now due to this, all food materials are full of chemicals and the life span of the people has been decreasing by generation. Our day to day life of earning is to satisfy the need of food. Now people are searching for organic food by the increase in awareness. The food now they eat is full of chemicals. The main aim of using herbicide is to kill the weeds in the agricultural land. But the drawback is that the herbicide is sprayed is all through the land. So the land also gets spoiled and the food also gets spoiled.
(6)Detection and Removal of Weed between Crops in Agricultural Field using Image Processing
International Journal of Pure and Applied Mathematics - Volume 118 No. 8 2018, 201-206 Abstract— Agriculture is the back bone of the entire world. In olden days, more than 70% of the people are depend on Agriculture and related works. But now the theme changed the people are preferring the white collar jobs and profit motive made them to refuse the agricultural work. In other end the demand of food products are increased due to growing population. Inorder to fulfill the needs of the food and the other agriculture products the Automation is the right choice. Not only in the field of agriculture, the automation plays a vital role in each and every field to produce Quality output with reduced manpower. In the field of agriculture the man power is need much when compared to other fields, from ploughing to harvesting. In this process the removal of weeds is a tedious job and it requires more man power. Till date the weed removal can’t be automated due to some effects in crops because of automation. Herbicides plays a crucial role in removing the weeds and it also provides a good result at initial position but later the weeds dominate the field. In order to reduce the weeds the usage herbicide level increased day by day. Usage of Herbicides causes the land infertile and thereby causes the decrease in production of the crops. This paper deals with the weed removal technique which reduces the amount of herbicide usage on the entire field. By this process the herbicides are sprayed on the weed. In this process the weed removal can be done through the image taken from the field by using image processing.
(7) WEED RECOGNITION SYSTEM FOR CROPS IN FARMS USING IMAGE PROCESSING TECHNIQUES AND SMART HERBICIDE SPRAYER ROBOT
ISSN (PRINT): 2393-8374, (ONLINE): 2394-0697, VOLUME-4, ISSUE-7, 2017 Abstract Most of the farmers in india uses conventional methods for the sprayer of herbicide in farms for cultivation of crops the major problems includes here is the sprayer is spread throughout all the crops in farms in which the cultivated plants are also harmed by chemicals sprayed on it as the weed and plants looks same,the weed recognition method based on the image processing techniques and the automing the atic robot sprayer which will capture images and apply the morphological operations on images to identify the images to find the weeds from the plants. Dilation and Erosion methods are the methods used for weed recognition which helps to easily find the weeds and apply automatic sprayer on the weeds.The algorithm is based on erosion followed by dilation segmentation algorithm. This algorithm can detect weeds and also classify it. Currently the algorithm is tested on two types of images i.e. broad leaves images and narrow leaves images.
(8)Weed Detection Using Image Processing In Organic Farming through Aurdino
International Journal of Innovative Technology and Exploring Engineering (IJITEE) ISSN: 2278-3075, Volume-8 Issue-3, January 2019 Abstract: Programmed weed control is dependably a critical issue in natural cultivating. A basic advance in taking care of or destroying the weed pervasions among the vegetable harvests is the capacity to precisely and dependably separate weeds from crops. As of late, high spatial determination hyper unearthly imaging information from ground-based stages have demonstrated specific guarantee in this application. Weeds are expelled in a semi-organized developed field by utilizing picture preparing. The principal goal is to identify and remove weeds available with the developed plant and gather it independently. A camera is fixed for taking the pictures of a portion of the crops in the field. A direction of the weed is identified by picture handling utilizing MATLAB. The directions of the weed acquired from the camera are imparted through PC to the robot at last to cut the weeds.
- Sep 29 Fri 2017 10:24
SAS EG 筆記
20170929 星期五
(0) 基本概念
SAS程式是由DATA Step和PROC Step兩部份所組成。
SAS資料集之建立 例1
00001 DATA A;
00002 INPUT NAME $ 1-7 MATH 9-11;
00003 CARDS;
00004 GERGO 83
00005 ALERN 95
00006 CARMEL 63
00007 PROC PRINT;
00008 RUN;
例1中,第一行到 第三行稱為資料段,以DATA開頭,以CARDS 結束。
第一行告訴SAS:我們要建立一個資料集A,這個資料集可以是任何你想要取的名稱。
第二行是一個 INPUT敘述,告訴SAS第一個變數是NAME ,位於資料卡的第一欄到 第七欄,其中的$表示NAME是文字變數,第二個變數MATH位於資 料卡的9-11欄,是數字變數。
第三行之CARDS敘述表示資料步驟的敘述句到此結束,下面是資料的開始;
因此第4-6行即為資料,
第7 行是一個SAS程序步驟(PROC step),指示SAS去列印上述資料.印出最近一次使用到的data set,如要指定用 DATA=資料集名稱 指定要印出哪個資料集
範例2:
data test;
infile cards dlm=',@';
input accno acname $ balancce;
cards;
200000,Sara,20000
456000@Lisa@39000
;
proc print data=test; /* 螢幕輸出
data EGHOME.test1; /* 儲存在永久檔
set test;
run;
範例3:
libname lib名稱 '資料夾路徑'; data lib名稱.新sas7bdat檔名; set lib名稱.原sas7bdat檔名
新變項一 = 原變項一 + 原變項二; 新變項二 = 原變項一 - 數字; 新變項三 = 原變項一 * 原變項二; 新變項四 = 原變項一 / 數字;
proc print data=lib名稱.新sas7bdat檔名; var id 新變項一 新變項二 新變項三 新變項四; run;
(1) SAS 永久檔(SAS Data Library):
SAS 永久資料檔為「兩階段命名」,所謂兩階段是指所有SAS永久資料檔都是由「Library 名稱」和「SAS 檔案名稱」組成,正式永久資料檔 的寫法應為「Library 名稱 . SAS 檔案名稱」,而所謂「Library 名稱」是資料夾的暱稱(nickname)或標籤(label),當沒設定 「Library」時,SAS 會自動將資料儲存於「WORK」的 Library,反之,若有設定 Library,SAS 會以永久檔的形式存在設定的「LIBNAME」所宣告的 Library(即資 料夾)中。
「LIBNAME」為宣告我們 要設定一個新的 Library,接下來的「mylib」為 Library 的名稱,然後再宣告這個 Library 在本機電腦的路徑,記得要使用引號將路徑包裹起來。
LIBNAME mylib "E:\SASNote\epaper\";
DATA mylib.esrd_data_082222;
SET esrd_02;
RUN;
註: 在「DATA set」中有兩個指令可以完成部份工作, 分別為「WHERE」和「IF」指令。
執行步驟:
(1.1).首先在 SAS 目錄下建立您自己的 Library 子目錄。 C:\SAS>WuLib
(1.2).進入 SAS 系統。
(1.3).以 LIBNAME 指令建立 Library reference。
LIBNAME WULIB 'C:\SAS\WuLib';
DATA WULIB.BODY;
INFILE 'C:\DATA\BODY.DAT';
INPUT W1 W2;
CARDS;
(1.4).執行以上程式後,可在 C:\SAS\WuLib 目錄下看到 BODY. sas7bdat 的 SAS data set。
範例:暫存檔
LIBNAME st 'C:\SAS\WuLib';
DATA aaa;
SET st.TYPE;
RUN;
永久檔
LIBNAME st 'C:\SAS\WuLib';
DATA st.aaa;
SET aa;
RUN;
(2)SET 敘述句
1.將資料集A的部份資料,複製到新的資料集B
data newname;
set oldname;
1.1.複製現存資料集中,部份變數
data newname;
set oldname;
keep (drop) 變數名;
1.2.複製現存資料集中,部份觀察值
data newname;
set oldname;
if … then delete;
2.以資料集A的資料產生新變數到新的資料集B
data newname;
set oldname;
new_var_name=經過運算元處理的舊變數;
3.將數個資料集縱向連結至一個新的資料集
data newname;
set oldname1 oldname2 oldname3 oldname4;
(3)資料輸入格式
(2.1)資料的輸入是以 INPUT 關鍵字開始,讀入哪些變數,各個變數的觀 測值在資料檔的位置等等
(2.1.1) 欄位輸入法(Column input)
此法輸入最省空間,有 missing values 也最容易看出,但所有資料必 需案所指定的格式排列整齊。
(2.2.1)行輸入(column input) 例1 即為行輸入,其方式是在變數名稱後指定其 所在欄位,如果是文字變數,則在變數名稱與欄位間加"$"來區別。
(2.2.2)列舉輸入(list input) 例2 為列舉輸入,此種方式僅要列出所有變數名稱,而文字變 數則只在變數名稱後加"$" 區別即可,但此種輸入方式必須注意 的是在資料卡上,每個變數值之間一定要以一 個或以上的空欄隔開,而且每個觀察值一定要依變數名稱排列之次序排列之。
DATA CLASS1;
INPUT NAME $ MATH;
(2.2.3)格式輸入(formatted input) 例3 為格式輸入,此種方式要以特定格式來指示 各變數值所在 的位置,可用的格式如下:
w. 表數字變數的長度為w
w.d 表數字變數的長度為w而其中小數佔d位
$w. 表文字變數的長度為w
@n 移動到第n個欄位 +n 移動n位
DATA CLASS2;
INPUT NAME $7. MATH 4.;
第二行表示第一個變數之名稱為NAME,為文字變數佔7 個欄位,第二個變數名稱為MATH,是數字變數佔4個欄位。
(2.2)INFILE 若欲輸入之資料不是列在資料步驟之下,而是存放在 SAS 系統以外的軟 碟或硬碟中,需用此句來指定。
(4)資料集輸出並列印在OUTPUT
PRINT的選擇項:【DATA=】宣告要印出的資料集
【ROUND】宣告取整數值
【NOOBS】宣告觀察體號碼不印出
【UNIFORM】宣告所有頁面均以同一格式印出
PRINT的關鍵字:【VAR】宣告要印出的變項
【BY】宣告以特定變數分開列印
DATA CASE;
INFILE 'C:\D001.txt' missover;
INPUT ID $ 1-4 Gend 5 age 6-7 Mar 8 Height 9-14 .1;
MISSING 999;
PROC PRINT DATA=CASE ROUND NOOBS UNIFORM;
TITLE '標題' ;
VAR ID age;
BY ID;
RUN;
- Sep 21 Thu 2017 14:04
Android 筆記(二)-AndroidManifest 說明
20170921 星期四
這篇文章從應用程式(Application)的高度來描述在開發Android APP時所需要的開發知識,每個應用程式的根目錄都必須包含 AndroidManifest.xml 檔案 (名稱要一字不差)。 宣示說明檔案可向 Android 系統顯示應用程式的基本資訊,也就是說系統在執行該應用程式的任何程式碼之前必須具備的資訊。
宣示說明檔AndrodiManifest.xml的檔案結構
下圖顯示宣示說明檔案的一般結構和可納入其中的元素。 每個元素和其所有屬性都會完全記錄在個別檔案中。
組成應用程式的元件,許多元件都會對應到 Java 物件,包括應用程式本身的元素 ( <application> 元素) 與其主要元件:Activity (<activity>)、服務 (<service>)、廣播接收器 (<receiver>) 以及內容供應程式 (<provider>)。為實作每個元件的類別命名以及發佈類別的功能 (例如,類別可處理的 Intent 訊息)。 這些宣告可讓 Android 系統瞭解元件為何以及可在哪些情況下啟動。除了能聲明程序中的Activities, ContentProviders, Services, 和Intent Receivers,還能指定permissions和instrumentation。
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<uses-permission/>
<permission/>
<permission-tree/>
<permission-group/>
<instrumentation/>
<uses-sdk/>
<uses-configuration/>
<uses-feature/>
<supports-screens/>
<compatible-screens/>
<supports-gl-texture/>
<application>
<activity>
<intent-filter>
<action/>
<category/>
<data/>
</intent-filter>
<meta-data/>
</activity>
<activity-alias>
<intent-filter> . . . </intent-filter>
<meta-data/>
</activity-alias>
<service>
<intent-filter> . . . </intent-filter>
<meta-data/>
</service>
<receiver>
<intent-filter> . . . </intent-filter>
<meta-data/>
</receiver>
<provider>
<grant-uri-permission/>
<meta-data/>
<path-permission/>
</provider>
<uses-library/>
</application>
</manifest>
Application元素的屬性
應用程式的宣告。應用程式元素可包括許多的子元素,許多子元素(例如 icon
, label
, permission
, process
, taskAffinity
, 和 allowTaskReparenting
)可以設定值,而這些值可以設定應用程式的名稱、圖示、標題、主題等資訊。例如「android:icon」屬性可指定這個應用程式的代表圖示。「android:label」屬性代表的是應用程式的名稱,另一個是「android:theme」屬性,theme意思是「主題」,在Android中將元件外觀的資訊我們稱為一個樣式(style),而將樣式套用在整個應用程式或單一畫面中,就稱為theme。
<?xmlversion="1.0"encoding="utf-8"?>
<manifest . . . >
<application . . . >
<activity android:name="com.example.project.FreneticActivity"
android:icon="@drawable/small_pic.png"
android:label="@string/freneticLabel" . . . >
</activity> . . . </application>
</manifest>
Activity元素的屬性
一個activity代表一個使用者介面者的螢幕視窗,簡單說一個Activity執行在螢幕上的動作,例如一個email 應用程式可能有一個activity 顯示新的emails列表,另外一個activity去編輯一個email, 而另一個activity去讀email的內容。假如一個應用程式有多個activity,我們則要在應用程式開啟時指定一個activity作動作。一個activity是被放置在Activity 類別內,定義如下所示 −
public class MainActivity extends Activity{
}
activity元素內還有「intent-filter」元素,代表該Activity類別會在本應用程式啟動時被第一個執行,也就是一個APP的第一個出現的畫面類別。
Intent Filter
Intent 可以被想像成 Android 當中的事件,用來串連 Android 中的 Activity 與 Service 等元件。每當一個 Activity 想執行一個操作時,會創建出一個 intent,該 intent 描述了 Activity 想要做甚麼事、想處理什麼類型的資料等。
Android 會比較 Intent 與 AndroidManifest.xml 中的 <intent-filter> 內容,以尋找最合適的 Activity 來處理回應 Intent 的請求。
Intent 可以直接指定元件名稱,Android 會根據 AndroidManifest.xml 檔案中的宣告找出該元件並啟動之。但是如果 Intent 沒有指定對象的元件名稱,Android 會透過 <intent-filter> 去找出最適當的元件回應 Intent ,<intent-filter> 指定了元件可以回應的 Intent 類型,
Services
用來處理與應用程式相關的背景程式。它在背景執行長時間的操作。例如當使用者在使用不同的應用程式時,一個服務可能在背景持續地播放音樂。它可能在背景執行擷取資料的工作,不會阻礙使用者和某一個activity的互動性。
一個serivce是被放置在Service 類別內,定義如下所示 −
public clas MyService extends Service{
}
Broadcast Receivers
用來處理Android OS與應用程式之間的通訊。
Content Providers
用來處理資料及資料庫管理的工作。
參考資料:https://developer.android.com/guide/topics/manifest/manifest-intro.html?hl=zh-tw
- Sep 18 Mon 2017 14:21
Android 筆記(一)-Hello World
20170918 星期一
下面三節我們從Android 的 MVC 架構和MVC 元件之間的互動、來了解如何撰寫Android程,有了設計架構概念後就能明白如何撰寫程式。
Android 的 MVC 架構
Android使用MVC架構,為了實現 MVC(Model-View-Controller)架構,Android 將 APP 的開發分成三大部分:
控制器 Controller - 負責轉發請求,對請求進行處理。
視圖 View - 介面設計人員進行圖形介面設計。
模型 Model - 編寫企業邏輯或程式應有的功能(實作演算法等等)、資料庫專家進行資料管理和資料庫設計(可以實作具體的功能)。
MVC 元件之間的互動
- 模型(Model) 用於封裝與應用程式的業務邏輯相關的資料以及對資料的處理方法。「 Model 」有對資料直接存取的權力,例如對資料庫的存取。「Model」不依賴「View」和「Controller」,也就是說, Model 不關心它會被如何顯示或是如何被操作。但是 Model 中資料的變化一般會通過一種重新整理機制被公布。為了實現這種機制,那些用於監視此 Model 的 View 必須事先在此 Model 上註冊,從而,View 可以了解在資料 Model 上發生的改變。(比較:觀察者模式(軟體設計模式))
- 視圖(View)能夠實現資料有目的的顯示(理論上,這不是必需的)。在 View 中一般沒有程式上的邏輯。為了實現 View 上的重新整理功能,View 需要存取它監視的資料模型(Model),因此應該事先在被它監視的資料那裡註冊。
- 控制器(Controller)起到不同層面間的組織作用,用於控制應用程式的流程。它處理事件並作出回應。「事件」包括用戶的行為和資料 Model 上的改變。
參考資料: https://upload.wikimedia.org/wikipedia/commons/f/f0/ModelViewControllerDiagramZh.png
MVC 元件與目錄結構
Android 的程式架構
Android的專案,主要可分為3個部分:分別是 manifest、java 和 res
1. manifest
Android App 整體資訊的主要設定檔,manifests 目錄內有一個預設名稱為 AndroidManifest.xml 的檔案,而每一個 Android APP 都須要它,此檔案儲存著該 APP 的重要資訊。設定檔的內容使用許多XML格式的檔案,不同用途的檔案,會使用標籤(tag)來執行一些設定。Android APP 中會包含許多 XML 格式,檔名中的 Manifest 為「表明」的意思。這是一個可以向 Android 「表明」我們 APP 的檔案,以便讓 Android 系統完整地了解我們 APP 的資訊。
2. java
放置 Java 原始程式,子目錄名稱為 [Company Domain(inverse) + Application name],裡面有一個預設的 Activity 元件 MainActivity.java。子目錄名稱,就是建立專案時由 Company Domain 反向後,在與 Application name 組成的 Package name,裡面有一個預設的 Activity 元件 MainActivity.java。
3. res
為 resource 的縮寫,專案所需的 UI 相關檔案,如 layout、圖像與文字。
drawable : 圖像檔案資源,例如 png、jpg……圖形檔案
layout : UI(使用者介面) 的 layout,例如在「res/layout」目錄下,已經建立一個名稱為「activity_main.xml」的預設畫面配置檔案。
values : UI 需用到的文字 (strings.xml)、顏色等一些常數資料
下面我們利用網路上常用的 HelloWorld 程式來大略看一下Android 的程式架構.一個最單純的 Android Project (HelloWorld) 會包含哪些檔案.將 HelloWorld 點開. 應該可以看到如下的資料結構:
src : 放置主程式、class 的地方.
java : 放系統資源檔、元件存放的地方. 當中的 R.java 是由 Android Development Kit (ADK) 所自動產生的資源索引檔(resource index). R.java 是根據 main.xml (在 res.layout中) 所自動產生,並不是由程式設計師編寫的,切勿修改此檔案。
res: 外部資源檔存放的位置, 可以設定文字、圖片、XML等
drawable : 存放 icon圖片的地方。layout : 存放畫面設定檔,values : 存放常數資源檔。
另外還有一個「Gradle Scripts」目錄,儲存與Gradle建置系統相關的設定檔,Gradle是Android Studio採用的全新應用程式建置系統。在Android Studio開發Android應用程式,一個應用程式可以有多個模組(Module)。例如一個音樂播放應用程式,可以包含行動電話、平板電腦、穿戴式三個模組,每一個模組都可以被建置成一個獨立的App。在Gradle Scripts目錄有下列兩個主要的設定檔,它們都是Groovy格式的文字檔:
•build.gradle(Project:專案名稱):應用程式最頂端的Gradle設定檔。以前面建立的應用程式專案來說,名稱是「build.gradle(Project:HelloAndroid)」。
•build.gradle(Module:模組名稱):每一個模組的Gradle設定檔。以前面建立的應用程式專案來說,名稱是「build.gradle(Module:app)」。
Android程式開發相關名詞
• Activity:在Android上執行的一個處理程序(不一定有畫面)稱之(類似於.NET的Form表單)
• View:Activity上的顯示元件,可以組成UI(類似於.NET的Control物件)
• Component: 組成Android程式的基礎,包括Activity、Service、Broadcast Receiver、Content provider
• Service:沒有UI的執行程序(例如在背景放音樂)
• Broadcast Receiver:負責接收廣播訊息,例如時區更新、電池儲量等資訊
• Content provider:資料提供者,例如檔案與SQLite資料庫
• Intent:啟動Component的發動者,可看成是對應的處理事件(.NET程式的稱呼)
#---------------------------------
下面我們將錄如何快速地建立一個專案,編輯內容及執行專案。這些步驟可以不是一步步的設定步驟,但最主要是要用一種概況的方式來記錄過程,所以有些不明白步驟需要再查詢其他文件。
參考資料 http://androidstation.pixnet.net/blog/post/212863265
第一步驟 : 我的第一個Android程式-設定專案資訊
其它欄位使用預設值即可,最後按下Next按鈕。
第二步驟 : 設定專案Activity資訊 (下面為Controller設計)
<?xml version="1.0" encoding="utf-8"?>
<resources>
…(其它程式碼)
<string name="hello_world ">你好,這個程式的主要類別名稱叫做MainActivity</string>
</resources>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextViewstyle="@style/Widget.SampleMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/horizontal_page_margin"
android:layout_marginRight="@dimen/horizontal_page_margin"
android:layout_marginTop="@dimen/vertical_page_margin"
android:layout_marginBottom="@dimen/vertical_page_margin"
android:text="@string/intro_message"/>
<GridViewandroid:id="@android:id/list"
style="@style/Widget.SampleDashboard.Grid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="@dimen/horizontal_page_margin"
android:paddingRight="@dimen/horizontal_page_margin"
android:paddingBottom="@dimen/vertical_page_margin"
android:scrollbarStyle="outsideOverlay"/>
</LinearLayout>
- Sep 11 Mon 2017 12:02
Python 常用指令及副程式收集
(1) 儲存檔案
with open("Output.txt","w")as text_file:
text_file.write("Purchase Amount: {0}".format(TotalAmount))
或 > = python2.7 可以使用 {} 代替 {0}
with open("Output.txt","w")as text_file:
print("Purchase Amount: {}".format(TotalAmount))
(2) 字串的運算: 字串資料也支援下列幾種運算方式
(2.1) 字串相加 : 兩個字串資料作相加(+
)的運算
x = 'Hello, '
y = 'World'
print x+y
這段程式碼會印出 Hello, World
(2.2)產生重複字串
字串資料可以與整數進行 *
的運算,這表示要將字串重複幾次:
x = 'Cat'
print x * 3
這樣螢幕會輸出 CatCatCat
(2.3)取出字串部份內容
想要取出字串部份的內容,不管是單一字元,或是一段範圍的字串,可以使用 []
運算子來操作:
x = 'Hello, World.'
print x[7]
print hello[0:6]
這段程式碼會分別在螢幕上印出 "W
" 及 "Hello,
"
(2.4)計算字串長度
如果要計算字串的長度,直接使用 len()
函式就可以了。
x = 'Hello, World.'
print len(x)
就會在螢幕上印出 13
。
(3)使用 List 資料
List 資料型態可以用來表示或處理排列在一起的資料,在 Python 裡是使用中括號( [
及 ]
)包起來表示,而 List 裡的資料可以是任何資料型態:記住,List 資料的索引是*從0
開始**
a = [1, 3, 'abc', 7, "xyz"]
print a[3]
螢幕上印出: 7
(3.1)選取某個元素
單一索引值取出 List 資料中的某一份資料
(3.2)選取一段範圍
除了使用單一索引值取得 List 中單一元素,也可以設定範圍取出一段部份資料,如:
print a[1:3]
螢幕上印出: [3, 'abc']
(4)Dictionary
建立一個 Dictionary 物件
d = {'python': 'Large constricting snakes.', 'ruby': 'A precious stone that is a red corundum.'}
使用 []
這個運算子的時候,我們就可以拿到它的 Value 了
print d['python']
則會印出
Large constricting snakes.
(4.1)取得 Dictionary 物件中 Key 的個數
print len(d)
則會印出:2
(4.2)新增或修改一筆資料
d['game'] = 'Activity engaged in for diversion or amusement.'
print d
(4.3)刪除一筆資料
del d['game']
print d
- Sep 08 Fri 2017 11:15
HTTP Request 方法: Get 和 Post
在Web的世界中, client 和 server 兩者之間最常做的動作就是 Request-Response(要求-回應),用戶端送一個要求(Request),而伺服器回應訊息(Response)。
所以我們要了解HTTP Request的方法。HTTP Request 的方法有二種: GET and POST,這二者之間有何差別。
HTTP Request 方法二種:
(1) Get 方法:當表單 Submit 之後瀏覽器的網址就變成 "/test/demo_form.php/?id=010101",瀏覽器會自動將表單內容轉為 Query String 加在 URL 進行連線。
客戶端傳送形式: <form action="接收資料的程式" method="get">
伺服器接收形式:這個鍵值配對會被存入request.GET
中,用request.GET['變數名']
可以得到值
- GET requests 會儲存在快取區。
- GET requests 會儲在客戶端瀏覽器的歷史區。
- GET requests 可以被標示。
- GET requests 不要使用在敏感性資料。
- GET requests 有長度限制。
- GET requests 應該僅使用在擷取資料。
- GET requests 資料會顯示在URL上。
- GET requests 網址會帶有 HTML Form 表單的參數與資料。
- GET requests 只接受ASCII字符。
(2) Post 方法:POST 是將表單資料放在 message-body 進行傳送。
客戶端傳送形式: <form action="接收資料的程式" method="post">
伺服器接收形式:這個鍵值配對會被存入request.POST
中,用request.POST['變數名']
可以得到值
- POST requests 不會儲存在快取區
- POST requests 不會儲在客戶端瀏覽器的歷史區
- POST requests 不可以被標示。
- POST requests 沒有長度限制。
- POST requests 資料傳遞時,網址並不會改變。
- POST requests 字符類型没有限制。
注意: GET 的URL 會顯示出Query 字串,而Query 字串內包含變數名/資料,所以會比較不安全,建議含有密碼的資訊不要使用這種方法。
- Sep 06 Wed 2017 09:33
Django 筆記(六)- Templates深入研究
這篇文章中我們將說明如何在Templates 中如何與Javascript,CSS、JQuery互動。 一個網頁除了後端資料庫外,我想另一個最重要的是網頁前端外觀的呈現,而網頁前端的工作則要利用Javascript,CSS、JQuery等語言去處理,因此我需要在Templates的使用上多記錄一些東西,另外是否要學習Templates language。
我經常看這個圖然後思考一些資料流傳送的問題
(1) 在 Views 中的資料如何傳送至Templates ?
(2) 在 Views 中如何使用Models 的資料 ?
(3) 在 Templates 如何使用Javascript 及 JQuery ?
以上這些問題都在章節都有答案。但還有下面問題,Templates 是前端程式,而Views 是後端程式
(4) 在 Templates中如何宣告變數及設定變數值初值 ?
答案: 有二種方法
(1)用with template tag 方法
例子1 : 設定初值
{% with name="World" greeting="Hello" %}
<html>
<div>{{ greeting }} {{name}}!</div>
</html>
{% endwith %}
例子2 : 將Models 中的值帶進來
{%with restaurant_id=restaurant.id %}
... use restaurant_id in this template section ...
{% endwith %}
(2)
{% my_var = "My String" %}
然後使用
<h1>{% trans my_var %}</h1>
或者
{% trans "my string" as my_var %}
{{ my_var }} {# 你可以使用my_var 變數 #}
(5) Templates 中的變數值可以傳送至Views 嗎?
答案: 有四種方法
1. Post
傳送內容有值的form
# 在views.py 中經由
request.POST.get('value')
2. Query Parameters
使用 //localhost:8000/?id=123
# 在views.py 中經由
request.GET.get('id')
3. From the URL (See here for example)
使用 //localhost:8000/12/results/
#
在urls.py 中經由
urlpatterns = patterns(... url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),...)
# 在 views 中得到 (question_id)
def detail(request, question_id):
...
returnHttpResponse("blahblah")
4. Session (使用 cookie)
Downside of using session is you would have had to pass it to the view or set it earlier.
https://docs.djangoproject.com/en/1.7/topics/http/sessions/
# views.py
# 設定session變數
request.session['uid']=123456
# 擷取session變數
var= request.session.get['uid']
(6)使用JQuery 傳送變數值至Views(使用GET)
在urls.py 中新增
url(r'^list/$', list),
url(r'^list_ret/$', list_ret)
在views.py 中新增
def list(request):
return render(request, 'list.html')
def list_ret(request):
list_ret_data = request.GET.get('list')
return render(request, 'list_ret.html')
新增list.html
{% load staticfiles %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Django/jQuery AJAX</title>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
<script src="{% static "jquery-3.2.1.js" %}"></script>
<script src="{% static "jquery-ui-1.12.1.zip" %}"></script>
<! ------- must care the path --------------------------------------------!>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"> </script>
<script type="text/javascript">
function list_Btn() {
var list = $('#right').text();
alert(list);
$.ajax({
url: '/list_ret/',
type: 'get',
data :{'list' : list},
success: function(msg) {
alert('結果'+msg);
},
error: function(data){
alert('status:'+data.status); // the status code
alert(data.responseJSON.error); // the message
}
});
}
</script>
</head>
<div id = 'right' style="width: 25%; float:right;text-align:left;font-size:20px;">
資料傳送開始
<br>
<input id = 'song_Btn' type ="button" value="傳送" onClick="list_Btn()"></input>
</div>
</html>
新增list_ret.html
傳送資料回覆(任何傳送前端內容)
{{list_ret_data}}
這是一個最簡單的一個範例,我們可以在 class list_ret 中接收到前端所傳送過來的所有資料,然後我們就可以在後端做資料庫處理,作檔案操縱,作企業邏輯運算,作任何在後端可以做的事,運算完後將結果放入一個中間網頁再回傳到原來呼叫的網頁,如此就可以不用改變前端網頁內容而做到後端處理的非同步網頁處理方式。
<6> 從Views 傳送一個dict 去Templates
views
def main(request):
ord_dict['2015-06-20']={}
ord_dict['2015-06-20']['a']='1'
ord_dict['2015-06-20']['b']='2'
ord_dict['2015-06-21']={}
ord_dict['2015-06-21']['a']='10'
ord_dict['2015-06-21']['b']='20'
return render(request, 'list.html',locals())
Templates(由小排至大)
<table>
{%for key, value in context.items.items|dictsort:"0.lower" %}
<tr> <td>{{ key }}</td><td>{{ value.a }}</td>
<td>{{ value.b }}</td>
</tr>
{% endfor %}</table>
<6> 從Views 傳送一個list 去Templates
views
def main(request):
return render(request, 'list.html',locals())
Templates
<table>
<tr> <td>{{ key }}</td>
</tr>
{% endfor %}</table>
<7> 從Views 傳送一個含有中文字的變數至Templates
在views 第一行寫# -*- coding: utf-8 -*-,並且檔案儲存格式存成utf-8格式
# -*- coding: utf-8 -*-
def main(request):
x1 = "中文"
return render(request, 'list.html',locals())
Templates 檔案儲存格式存成utf-8格式
{{X1}}
- Aug 28 Mon 2017 14:53
Django 筆記(五)-Django遇見Javascript和JQuery
2017.08.28
這篇文章中我們將說明在Django中如何使用Javascript,CSS、JQuery。
一個網頁除了後端資料庫外,我想另一個最重要的是網頁前端外觀的呈現,而網頁前端的工作則要利用Javascript,CSS、JQuery等語言去處理,因此這篇是很重要的參考文章。至於Javascript,CSS、JQuery等語言設計可能要另外參考資料了。
在 settings 中新增
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
(1)在專案目錄下新增一個staic 資料夾,與目錄須要配合上面的路徑設定,我們是要將js檔案放在os.path.join(BASE_DIR, 'static')
所以放在與 /tempates 同層次的目錄,這部份的設定是否有錯可參考http server的status 返回值是否顯示200或304,如顥示200或304表示路徑及檔案己成功找到並載入,如出現404則表示建錯目錄或找不到檔案。
(2)使用JavaScript不需要另外載入程式庫只要宣告就可以使用。
(3)JQuery程式庫可用二種方式載入,一種為檔案方式,一種為URL方式。
(4)檔案形式可於https://jquery.com/download/my 下載,下載.js 檔,min.js或壓縮檔皆可以。下載後放在/static資料夾。
(5)URL方式請檔案內設定URL路徑即可。
我們以之前的list.htm為例,在網頁內容內新增
{% load staticfiles %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>Django/jQuery AJAX</title>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
<script src="{% static "jquery-3.2.1.js" %}"></script>
<script src="{% static "jquery-ui-1.12.1.zip" %}"></script>
<! ------- must care the path --------------------------------------------!>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"> </script>
<script type="text/javascript">
function nothing() {
alert("查無任何資料,請重新查詢");
}
$( document ).ready(function() {
alert("Hello Jquery");
$("#msg").click(function(){
alert("Hello DIV");
});
});
</script>
</head>
<body>資料表內容<br>
{% for item in datas %}
<tr>
<td>{{ item.song_name }}</td>
<td>{{ item.songer }}</td>
</tr><br>
{% endfor %}
<input type ="button" value="submit" onClick="nothing()"></input> <br>
<div id='msg' style="text-align:center;font-size:20px;">
I am DIV, please click me <br>
</div>
</html>
如此Javascript 及JQuery 就可以在前端正常工作了。