7/10/2559
ตัวอย่างการเขียนโปรแกรมตัดคำภาษาไทยต้วย PHP
การตัดคำภาษาไทยนั้น projectsoft.biz ได้สร้างโปรแกรมบริการการตัดคำภาษาไทยที่
http://projectsoft.biz/cut_thai.php?word=คำภาษาไทยที่ต้องการตัด
สามารถใช้งานได้เลยโดยส่งค่าประโยคภาษาไทยแบบ GET เข้าไป ตาม URL
เช่น http://projectsoft.biz/cut_thai.php?word=อาหารร้านนี้อร่อย ผลลัพธ์ที่ได้คือ
อาหาร | ร้าน | นี้ | อร่อย |
หากนักพัฒนาต้องการ เชื่อมต่อ Web Application เพื่อใช้บริการตัดคำสามารถเรียกใช้ได้ตามตัวอย่าง
<?php
header('Content-Type: text/html; charset=utf-8');
$word=file_get_contents("http://projectsoft.biz/cut_thai.php?word=อาหารร้านนี้อร่อย");
echo $word;// $word จะมีค่าผลลัพธ์การตัดคำ คือ อาหาร | ร้าน | นี้ | อร่อย |
?>
6/16/2559
การ resize ภาพด้วย PHP
เพื่อเป็นการประหยัดทรัพยากรของ Server เว็บ Application ควรสามารถที่จะปรัปเปลี่ยนขนาดรูปภาพ ( resize )ของผู้ใช้ที่ upload มาไว้ที่ server ได้
ตัวอย่างการ resize ภาพโดยใช้ PHP
<?php
/*---------- การ resize ภาพ ----------*/
$original=imagecreatefromjpeg("picture.jpg");//สร้า object รูปภาพจากไฟล์ "picture.jpg"
$size=getimagesize ("picture.jpg");// หาขนาดภาพปัจจุบัน
$w_old=$size[0];//ความกว้างของ ภาพปัจจุบัน
$h_old=$size[1];//ความสูของ ภาพปัจจุบัน
$w=$w_old/5;//กำหนดความกว้างของ ภาพใหม่โดยกำหนดให้เป็น 1 ใน 5 ของความกว้างเดิม
$h=$h_old/5;//กำหนดความสูงของ ภาพใหม่โดยกำหนดให้เป็น 1 ใน 5 ของความสูงเดิม
$img_new_size=ImageCreateTrueColor($w ,$h);//สร้าง object ภาพใหม่ ชื่อว่า $img_new_size
//ทำการ resize ภาพ
imagecopyresized ( $img_new_size, $original, 0, 0, 0, 0, $w , $h , $w_old ,$h_old);
ImageJPEG($img_new_size,"new_picture.jpg");
//กำหนดให้ภาพที่ resize แล้ว ชื่อว่า "new_picture.jpg"
echo "<img src='picture.jpg' />";//แสดงรูปภาพก่อนเปลี่ยนขนาด
echo "<img src='new_picture.jpg' />";//แสดงรูปภาพหลังเปลี่ยนขนาด
?>
ผลลัพธ์ที่ได้ดังนี้ รูปด้านซ้ายคือ รูปก่อน resize ส่วนรูปขวาคือ รูปที่ resize แล้ว
จะเห็นว่าขนาดเป็น 1 ใน 5 ของรูปเดิม
ตัวอย่างการ resize ภาพโดยใช้ PHP
<?php
/*---------- การ resize ภาพ ----------*/
$original=imagecreatefromjpeg("picture.jpg");//สร้า object รูปภาพจากไฟล์ "picture.jpg"
$size=getimagesize ("picture.jpg");// หาขนาดภาพปัจจุบัน
$w_old=$size[0];//ความกว้างของ ภาพปัจจุบัน
$h_old=$size[1];//ความสูของ ภาพปัจจุบัน
$w=$w_old/5;//กำหนดความกว้างของ ภาพใหม่โดยกำหนดให้เป็น 1 ใน 5 ของความกว้างเดิม
$h=$h_old/5;//กำหนดความสูงของ ภาพใหม่โดยกำหนดให้เป็น 1 ใน 5 ของความสูงเดิม
$img_new_size=ImageCreateTrueColor($w ,$h);//สร้าง object ภาพใหม่ ชื่อว่า $img_new_size
//ทำการ resize ภาพ
imagecopyresized ( $img_new_size, $original, 0, 0, 0, 0, $w , $h , $w_old ,$h_old);
ImageJPEG($img_new_size,"new_picture.jpg");
//กำหนดให้ภาพที่ resize แล้ว ชื่อว่า "new_picture.jpg"
echo "<img src='picture.jpg' />";//แสดงรูปภาพก่อนเปลี่ยนขนาด
echo "<img src='new_picture.jpg' />";//แสดงรูปภาพหลังเปลี่ยนขนาด
?>
ผลลัพธ์ที่ได้ดังนี้ รูปด้านซ้ายคือ รูปก่อน resize ส่วนรูปขวาคือ รูปที่ resize แล้ว
จะเห็นว่าขนาดเป็น 1 ใน 5 ของรูปเดิม
5/09/2559
เขียนโปรแกรมดูตำแหน่ง Latitude Longitude ใน Android Application
ถ้าเราต้องการหาว่าตำแหน่ง Latitude Longitude ปัจจุบันของ Android Device เราสามารถรถใช้ LocationManager เพื่อหาตำแหน่ง Latitude Longitude ได้ดังนี้
1. ใน ไฟล์ androidmanifest.xml ให้เพิ่ม permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
เข้าไปใน <manifest>
2.ในไฟล์ XML Layout ให้เพิ่ม textview tag
ขึ้นมา เพื่อ แสดง Latitude Longitude
3. ในไฟล์ .java ให้ Import class เพื่อหา Latitude Longitude
4. ใน onCreate method ให้ เพิ่ม code เพื่อแสดง Latitude Longitude ดังนี้
1. ใน ไฟล์ androidmanifest.xml ให้เพิ่ม permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
เข้าไปใน <manifest>
2.ในไฟล์ XML Layout ให้เพิ่ม textview tag
<TextView android:id="@+id/lat_lng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
ขึ้นมา เพื่อ แสดง Latitude Longitude
3. ในไฟล์ .java ให้ Import class เพื่อหา Latitude Longitude
import android.location.Location; import android.location.LocationManager;
4. ใน onCreate method ให้ เพิ่ม code เพื่อแสดง Latitude Longitude ดังนี้
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try {
//สร้าง object สำหรับ หา latitude longitude
LocationManager lm = (LocationManager)getSystemService(this.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();//รับค่า longitude
double latitude = location.getLatitude();//รับค่า latitude
//แสดง latitude longitude ใน TextView
TextView t=(TextView)findViewById(R.id.lat_lng);
t.setText("Longitude:"+String.valueOf(latitude)+"\nLongitude:"+String.valueOf(longitude));
}
catch (SecurityException e)
{
}
}
4/30/2559
ตัวอย่าง image processing segmentation ด้วย javascritp และ HTML5
สำหรับโปรเจคเกี่ยวกับ image processing นั้น เราสามารถให้โปรแกรมรันบนบน browser ได้แล้วโดยใช้ HTML5 ซึ่งสามารถทำให้เราทำ image processing บน Web Browser ได้
ตัวอย่าง การทำ segmentation และ ใช้ otsu's method ในการหา threshold
<img src="1.jpg" id="pic" /><!----รูปภาพ -->
<br />
<button onclick="process()">Process</button><!--เรียก function process() เมื่อกด ปุ่ม Process -->
<br />
<script>
function otsu(histogram, pixelsNumber) { //function หาค่า threshold ด้วย otsu's method
var sum = 0
, sumB = 0
, wB = 0
, wF = 0
, mB
, mF
, max = 0
, between
, threshold = 0;
for (var i = 0; i < 256; ++i) {
wB += histogram[i];
if (wB == 0)
continue;
wF = pixelsNumber - wB;
if (wF == 0)
break;
sumB += i * histogram[i];
mB = sumB / wB;
mF = (sum - sumB) / wF;
between = wB * wF * Math.pow(mB - mF, 2);
if (between > max) {
max = between;
threshold = i;
}
}
return threshold;
}
function process()//function สำหรับทำ segmentation
{
//สร้าง canvas และ เก็บภาพต้นฉบับ
var im = document.getElementById("pic")
, cnv = document.createElement('canvas')
, ctx = cnv.getContext('2d');
//หาค่า threshold
cnv.width = im.width;
cnv.height = im.height;
ctx.drawImage(im, 0, 0);
var imData = ctx.getImageData(0, 0, cnv.width, cnv.height)
, histogram = Array(256)
, i
, red
, green
, blue
, gray;
for (i = 0; i < 256; ++i)
histogram[i] = 0;
for (i = 0; i < imData.data.length; i += 4) {
red = imData.data[i];
blue = imData.data[i + 1];
green = imData.data[i + 2];
gray = red * .2126 + green * .07152 + blue * .0722;
histogram[Math.round(gray)] += 1;
}
var threshold = otsu(histogram, imData.data.length / 4);
//สำหรับ pixel ที่มีค่า ต่ำกว่า threshold กำหนดให้เป็น 0 ถ้ามากกว่าหรือเท่ากับ threshold ให้เป็น 255
for (i = 0; i < imData.data.length; i += 4) {
imData.data[i] = imData.data[i + 1] = imData.data[i + 2] =
imData.data[i] >= threshold ? 255 : 0;
imData.data[i + 3] = 255;
}
ctx.putImageData(imData, 0, 0);
document.body.appendChild(cnv);//เพิ่ม canvas ผลการ แบ่ง Segment เข้าไปในหน้าเว็บ
}
</script>
3/24/2559
การเชื่อมต่อหน้าใน ionic framework
Ionic framework นับว่าเป็น frame work สำหรับพัฒนา mobile application ที่มีประสิทธิภาพมาก เหมาะสำหรับการพัฒนา mobile application ด้วย html และ javascript
ปัญหาสำหรับนักพัฒนาที่เพิ่งใช้ ionic frawork คือการ link หน้าต่างๆใน application ในที่นี้คือตัวอย่างการ link หน้า app สำหรับ ionic framework
1. Download Ionic Framework จาก http://projectsoft.biz/ionic.zip และแตกโฟลเดอร์ ชื่อ ionic ไว้ใน folder เดียวกับ index.html (ไฟล์เริ่มต้น)
2. สร้างไฟล์ index ดังนี้
<!DOCTYPE html>
<html ng-app="ionicApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<!-- เรียกใช่ ionic framework -->
<link rel="stylesheet" type="text/css" href="ionic/css/ionic.min.css">
<script type="application/javascript" src="ionic/js/ionic.bundle.js"></script>
<!-- เรียกใช่ ionic framework -->
</head>
<body ng-controller="main_control" >
<!--- สร้างวิว สำหรับ แสดงเนื้อหา app -->
<ion-nav-view></ion-nav-view>
<!-- เรียกไฟล์หน้า app แต่ละหน้า -->
<script src="page1.html" type="text/ng-template"></script>
<script src="page2.html" type="text/ng-template"></script>
<!---เรียกไฟล์หน้า app--->
<script src="index.js"></script>
<script src="page1.js"></script>
<script src="page2.js"></script>
</body>
</html>
2. สร้างไฟล์ index.js สำหรับควบคุมหน้า index.html
var app=angular.module('ionicApp', ['ionic']);
app.config(function($stateProvider,$urlRouterProvider) {
//กำหนด state ให้แต่ละ page
//ในที่นี้ page1 สำหรับ ไฟล์ page1.html และ page2 สำหรับไฟล์ page2.html
$stateProvider
.state('page1', {
url: "/",
templateUrl: 'page1.html'
})
.state('page2', {
url: "/",
templateUrl: 'page2.html'
});
});
app.controller('main_control', function($scope,$state) {
$state.transitionTo("page1");//กำหนดให้แสดงหน้า page1 เป็นหน้าแรก
});
3. สร้างไฟล์ page1.html
<ion-view >
<div ng-controller="page1_control">
<div class="bar bar-positive"><!-- กำหนด header ให้กับ หน้า app -->
<h1 class="title">หน้า 1</h1>
</div>
<ion-content class="content has-header " ><!--- แสดงเนื้อหาในหน้า app --->
<div class="padding" >
<h1>หน้า 1</h1>
<!-- สร้างปุ่มไปที่หน้า 2 -->
<button ng-click='go_page2()'
class="button icon-right ion-chevron-right button-assertive">หน้า 2</button>
</div>
</ion-content>
</div>
</ion-view>
<script>
//สร้างส่วน control สำหรับหน้า page1
app.controller('page1_control', function($scope,$state) {
$scope.go_page2=function()//กำหนดให้เมื่อกด ปุ่มแล้วจะ link ไปหน้า page2
{
$state.transitionTo("page2");
}
});
</script>
3. สร้างหน้า page2.html
<ion-view >
<div ng-controller="page2_control">
<div class="bar bar-positive">
<h1 class="title">หน้า 2</h1><!-- กำหนด header ให้กับ หน้า app -->
</div>
<ion-content class="content has-header " >
<div class="padding" >
<h1>หน้า 2</h1>
ปัญหาสำหรับนักพัฒนาที่เพิ่งใช้ ionic frawork คือการ link หน้าต่างๆใน application ในที่นี้คือตัวอย่างการ link หน้า app สำหรับ ionic framework
1. Download Ionic Framework จาก http://projectsoft.biz/ionic.zip และแตกโฟลเดอร์ ชื่อ ionic ไว้ใน folder เดียวกับ index.html (ไฟล์เริ่มต้น)
2. สร้างไฟล์ index ดังนี้
<!DOCTYPE html>
<html ng-app="ionicApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<!-- เรียกใช่ ionic framework -->
<link rel="stylesheet" type="text/css" href="ionic/css/ionic.min.css">
<script type="application/javascript" src="ionic/js/ionic.bundle.js"></script>
<!-- เรียกใช่ ionic framework -->
</head>
<body ng-controller="main_control" >
<!--- สร้างวิว สำหรับ แสดงเนื้อหา app -->
<ion-nav-view></ion-nav-view>
<!-- เรียกไฟล์หน้า app แต่ละหน้า -->
<script src="page1.html" type="text/ng-template"></script>
<script src="page2.html" type="text/ng-template"></script>
<!---เรียกไฟล์หน้า app--->
<script src="index.js"></script>
<script src="page1.js"></script>
<script src="page2.js"></script>
</body>
</html>
2. สร้างไฟล์ index.js สำหรับควบคุมหน้า index.html
var app=angular.module('ionicApp', ['ionic']);
app.config(function($stateProvider,$urlRouterProvider) {
//กำหนด state ให้แต่ละ page
//ในที่นี้ page1 สำหรับ ไฟล์ page1.html และ page2 สำหรับไฟล์ page2.html
$stateProvider
.state('page1', {
url: "/",
templateUrl: 'page1.html'
})
.state('page2', {
url: "/",
templateUrl: 'page2.html'
});
});
app.controller('main_control', function($scope,$state) {
$state.transitionTo("page1");//กำหนดให้แสดงหน้า page1 เป็นหน้าแรก
});
3. สร้างไฟล์ page1.html
<ion-view >
<div ng-controller="page1_control">
<div class="bar bar-positive"><!-- กำหนด header ให้กับ หน้า app -->
<h1 class="title">หน้า 1</h1>
</div>
<ion-content class="content has-header " ><!--- แสดงเนื้อหาในหน้า app --->
<div class="padding" >
<h1>หน้า 1</h1>
<!-- สร้างปุ่มไปที่หน้า 2 -->
<button ng-click='go_page2()'
class="button icon-right ion-chevron-right button-assertive">หน้า 2</button>
</div>
</ion-content>
</div>
</ion-view>
<script>
//สร้างส่วน control สำหรับหน้า page1
app.controller('page1_control', function($scope,$state) {
$scope.go_page2=function()//กำหนดให้เมื่อกด ปุ่มแล้วจะ link ไปหน้า page2
{
$state.transitionTo("page2");
}
});
</script>
3. สร้างหน้า page2.html
<ion-view >
<div ng-controller="page2_control">
<div class="bar bar-positive">
<h1 class="title">หน้า 2</h1><!-- กำหนด header ให้กับ หน้า app -->
</div>
<ion-content class="content has-header " >
<div class="padding" >
<h1>หน้า 2</h1>
<!-- สร้างปุ่ม link ไปที่หน้า 1 -->
<buttpn ng-click='go_page1()' class="button icon-left ion-chevron-left button-assertive">หน้า 1</button>
</div>
</ion-content>
</div>
</ion-view>
<script>
//สร้างส่วน control สำหรับหน้า page2
app.controller('page2_control', function($scope,$state) {
$scope.go_page1=function()
{
$state.transitionTo("page1");//กำหนดให้เมื่อกด ปุ่มแล้วจะ link ไปหน้า page1
}
});
</script>
ผลลัพธ์ที่ได้คือ
<buttpn ng-click='go_page1()' class="button icon-left ion-chevron-left button-assertive">หน้า 1</button>
</div>
</ion-content>
</div>
</ion-view>
<script>
//สร้างส่วน control สำหรับหน้า page2
app.controller('page2_control', function($scope,$state) {
$scope.go_page1=function()
{
$state.transitionTo("page1");//กำหนดให้เมื่อกด ปุ่มแล้วจะ link ไปหน้า page1
}
});
</script>
ผลลัพธ์ที่ได้คือ
หน้า 1
หน้า 2
3/18/2559
SharedPreferences ตัวแปรที่ใช้ได้หลาย Activity สำหรับเก็บข้อมูลลงใน Android คล้าย Session ของ PHP
สำหรับ Web Programmer ที่เคยใช้ PHP นั้น ย่อมคุ้นชินกับการใช้ตัวแปร ประเภท Session ที่เก็บค่าตัวแปรแล้วสามารถเข้าถึงตัวแปรใน File อื่นได้ สำหรับการเขียนโปรแกรมบน Android นั้นเราสามารถทำแบบนั้นได้เช่นกันโดยใช้ Class ที่ชื่อว่า SharedPreferences
//สร้าง object SharedPreferences สำหรับเก็บข้อมูล
SharedPreferences sp = getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
//PREF_NAME เป็นชื่อ Shared Preferences ใช้ชื่ออะไรก็ได้
//Context.MODE_PRIVATE คือบอกว่าจะใช้แค่ในแอปพลิเคชันนี้เท่านั้น
String id =sp.getString("id", "0");
//อ่านค่าจาก object preferences โดยให้ค่า 0 (Default) หากยังไม่ได้กำหนด SharedPreferences
SharedPreferences.Editor editor = sp.edit();
editor.putString("id","1");//กำหนดค่าให้กับ SharedPreferences ที่ชื่อ id เป็น 1
editor.commit();//บันทึกค่า SharedPreferences
String id =sp.getString("id", "0");//ในกรณีนี้ id จะมีค่าเป็น 1 เพราะได้มีการกำหนดค่าแล้ว
การหาวันที่ถัดไป ด้วย javascript
ในบางโปรเจค ระบบต้องการรู้ว่าวันที่ถัดไป n วันนั้นเป็นวันที่เท่าไหร่ เช่นในระบบเช่า ถ้าเช่าจำนวน n วันระบบต้องแสดงว่าต้องคืนในวันที่เท่าไหร่ เราสามารถใช้ javascritp หาได้ว่าวันถัดไปนั้นที่วันที่เท่าไหร่ดังตัวอน่างต่อไปนี้
var today=new Date();//สร้างตัวแปรวันที่ปัจจุบัน
var tomorrow = new Date();//;สร้างตัวแปรวันพรุ่งนี้
tomorrow.setDate(today.getDate()+1); //กำหนดให้ tomorrow คือวันถัดไป
document.write(tomorrow.getDate());//พิมพ์วันที่
document.write((tomorrow.getMonth()+1));
//พิมพ์เดือน โดย javascript นั้นจะกำหนดให้ เดือนเริ่มต้นที่ 0 จึงต้องบวกด้วย 1
document.write(tomorrow.getFullYear());//พิมพ์ปี
var today=new Date();//สร้างตัวแปรวันที่ปัจจุบัน
var tomorrow = new Date();//;สร้างตัวแปรวันพรุ่งนี้
tomorrow.setDate(today.getDate()+1); //กำหนดให้ tomorrow คือวันถัดไป
document.write(tomorrow.getDate());//พิมพ์วันที่
document.write((tomorrow.getMonth()+1));
//พิมพ์เดือน โดย javascript นั้นจะกำหนดให้ เดือนเริ่มต้นที่ 0 จึงต้องบวกด้วย 1
document.write(tomorrow.getFullYear());//พิมพ์ปี
สมัครสมาชิก:
บทความ (Atom)





