|
本文将介绍C/S模式的图像处理系统。C/S的框架已经在[1]中作了简单的介绍。[2]中介绍了如何搭建基于android和WAMP5的B/S模式的本机测试平台。本系统是在[4]中介绍的基础上开发的,有关图像显示和本地图像处理的框架可以参看[4]; @author:郑海波 zhb931706659@126.com
转载请声明:http://blog.iyunv.com/nuptboyzhb/article/details/7945249
实验结果展示:(图像的DCT变换)
实验平台: 服务器:Windows 7+Apache+MySQL+PHP 客户端:Android 开发工具:Eclipse+ADT+AVD 我们先看客户端(Client)的android开发 1.图像处理框架: 本次试验的图像处理框架我们依然使用[4]的实验平台。该实验平台已经实现了图像的打开,保存,摄像,截屏,显示和简单的图像处理算法等功能。 我们本次试验,需要用到该框架的图像打开,显示,保存等基本功能。 2.客户端的核心 在客户端,C/S模式的核心就是如何将本机数据发送到服务器,并从服务器中下载处理完后的数据。由于连接服务器,发送,接收等需要较长时间,因此 我们将其过程封装到一个AsyncTask派生的类中,代码如下: [java code]
[java] view plaincopyprint?
- publicclass ServerTask extends AsyncTask
- {
- publicbyte[] dataToServer;
-
- //Task state
- privatefinalint UPLOADING_PHOTO_STATE = 0;
- privatefinalint SERVER_PROC_STATE = 1;
-
- private ProgressDialog dialog;
- //upload photo to server
- HttpURLConnection uploadPhoto(FileInputStream fileInputStream)
- {
-
- //final String serverFileName = "test"+ (int) Math.round(Math.random()*1000) + ".jpg";
- final String serverFileName = "test.jpg";
- final String lineEnd = "\r\n";
- final String twoHyphens = "--";
- final String boundary = "*****";
- Log.e("msg","begin HttpURLConnection......");
- try
- {
- URL url = new URL(SERVERURL);
- // Open a HTTP connection to the URL
- final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- // Allow Inputs
- conn.setDoInput(true);
- // Allow Outputs
- conn.setDoOutput(true);
- // Don't use a cached copy.
- conn.setUseCaches(false);
-
- // Use a post method.
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
-
- DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
-
- dos.writeBytes(twoHyphens + boundary + lineEnd);
- dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + serverFileName +"\"" + lineEnd);
- dos.writeBytes(lineEnd);
-
- // create a buffer of maximum size
- int bytesAvailable = fileInputStream.available();
- int maxBufferSize = 1024;
- int bufferSize = Math.min(bytesAvailable, maxBufferSize);
- byte[] buffer = newbyte[bufferSize];
-
- // read file and write it into form...
- int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
-
- while (bytesRead > 0)
- {
- dos.write(buffer, 0, bufferSize);
- bytesAvailable = fileInputStream.available();
- bufferSize = Math.min(bytesAvailable, maxBufferSize);
- bytesRead = fileInputStream.read(buffer, 0, bufferSize);
- }
-
- // send multipart form data after file data...
- dos.writeBytes(lineEnd);
- dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
- publishProgress(SERVER_PROC_STATE);
- // close streams
- fileInputStream.close();
- dos.flush();
- Log.e("msg","upload finished!");
- return conn;
- }
- catch (MalformedURLException ex){
- Log.e(TAG, "error: " + ex.getMessage(), ex);
- returnnull;
- }
- catch (IOException ioe){
- Log.e(TAG, "error: " + ioe.getMessage(), ioe);
- returnnull;
- }
- }
-
- //get image result from server and display it in result view
- void getResultImage(HttpURLConnection conn){
- // retrieve the response from server
- InputStream is;
- try {
- is = conn.getInputStream();
- //get result image from server
- resultForWebImage = BitmapFactory.decodeStream(is);
- is.close();
- IsShowingResult = true;
- Log.d("msg","download finished!");
- } catch (IOException e) {
- Log.e(TAG,"getResultImage:"+e.toString());
- e.printStackTrace();
- }
- }
- //Main code for processing image algorithm on the server
- void processImage(String inputImageFilePath){
- publishProgress(UPLOADING_PHOTO_STATE);
- File inputFile = new File(inputImageFilePath);
- try {
-
- //create file stream for captured image file
- FileInputStream fileInputStream = new FileInputStream(inputFile);
-
- //upload photo
- final HttpURLConnection conn = uploadPhoto(fileInputStream);
-
- //get processed photo from server
- if (conn != null){
- getResultImage(conn);
- //String download="http://10.10.145.154/EE368_Android_Tutorial3_Server/computeSIFTOnSCIEN.php";
-
- }
- fileInputStream.close();
- }
- catch (FileNotFoundException ex){
- Log.e(TAG, "processImage"+ex.toString());
- }
- catch (IOException ex){
- Log.e(TAG, "processImage"+ex.toString());
- }
- }
-
- public ServerTask() {
- dialog = new ProgressDialog(SystemMain.this);
- }
-
- protectedvoid onPreExecute() {
- this.dialog.setMessage("Photo captured");
- this.dialog.show();
- }
- @Override
- protected Void doInBackground(String... params) { //background operation
- String uploadFilePath = params[0];
- processImage(uploadFilePath);
- //release camera when previous image is processed
- mCameraReadyFlag = true;
- returnnull;
- }
- //progress update, display dialogs
- @Override
- protectedvoid onProgressUpdate(Integer... progress) {
- if(progress[0] == UPLOADING_PHOTO_STATE){
- dialog.setMessage("Uploading");
- dialog.show();
- }
- elseif (progress[0] == SERVER_PROC_STATE){
- if (dialog.isShowing()) {
- dialog.dismiss();
- }
- dialog.setMessage("Processing");
- dialog.show();
- }
- }
- @Override
- protectedvoid onPostExecute(Void param) {
- if (dialog.isShowing()) {
- dialog.dismiss();
-
- if(IsShowingResult){
- ShowImage(resultForWebImage, 2);
- IsShowingResult=false;
- }
- }
- }
- }
public class ServerTask extends AsyncTask
{
public byte[] dataToServer;
//Task state
private final int UPLOADING_PHOTO_STATE = 0;
private final int SERVER_PROC_STATE = 1;
private ProgressDialog dialog;
//upload photo to server
HttpURLConnection uploadPhoto(FileInputStream fileInputStream)
{
//final String serverFileName = "test"+ (int) Math.round(Math.random()*1000) + ".jpg";
final String serverFileName = "test.jpg";
final String lineEnd = "\r\n";
final String twoHyphens = "--";
final String boundary = "*****";
Log.e("msg","begin HttpURLConnection......");
try
{
URL url = new URL(SERVERURL);
// Open a HTTP connection to the URL
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + serverFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
publishProgress(SERVER_PROC_STATE);
// close streams
fileInputStream.close();
dos.flush();
Log.e("msg","upload finished!");
return conn;
}
catch (MalformedURLException ex){
Log.e(TAG, "error: " + ex.getMessage(), ex);
return null;
}
catch (IOException ioe){
Log.e(TAG, "error: " + ioe.getMessage(), ioe);
return null;
}
}
//get image result from server and display it in result view
void getResultImage(HttpURLConnection conn){
// retrieve the response from server
InputStream is;
try {
is = conn.getInputStream();
//get result image from server
resultForWebImage = BitmapFactory.decodeStream(is);
is.close();
IsShowingResult = true;
Log.d("msg","download finished!");
} catch (IOException e) {
Log.e(TAG,"getResultImage:"+e.toString());
e.printStackTrace();
}
}
//Main code for processing image algorithm on the server
void processImage(String inputImageFilePath){
publishProgress(UPLOADING_PHOTO_STATE);
File inputFile = new File(inputImageFilePath);
try {
//create file stream for captured image file
FileInputStream fileInputStream = new FileInputStream(inputFile);
//upload photo
final HttpURLConnection conn = uploadPhoto(fileInputStream);
//get processed photo from server
if (conn != null){
getResultImage(conn);
//String download="http://10.10.145.154/EE368_Android_Tutorial3_Server/computeSIFTOnSCIEN.php";
}
fileInputStream.close();
}
catch (FileNotFoundException ex){
Log.e(TAG, "processImage"+ex.toString());
}
catch (IOException ex){
Log.e(TAG, "processImage"+ex.toString());
}
}
public ServerTask() {
dialog = new ProgressDialog(SystemMain.this);
}
protected void onPreExecute() {
this.dialog.setMessage("Photo captured");
this.dialog.show();
}
@Override
protected Void doInBackground(String... params) {//background operation
String uploadFilePath = params[0];
processImage(uploadFilePath);
//release camera when previous image is processed
mCameraReadyFlag = true;
return null;
}
//progress update, display dialogs
@Override
protected void onProgressUpdate(Integer... progress) {
if(progress[0] == UPLOADING_PHOTO_STATE){
dialog.setMessage("Uploading");
dialog.show();
}
else if (progress[0] == SERVER_PROC_STATE){
if (dialog.isShowing()) {
dialog.dismiss();
}
dialog.setMessage("Processing");
dialog.show();
}
}
@Override
protected void onPostExecute(Void param) {
if (dialog.isShowing()) {
dialog.dismiss();
if(IsShowingResult){
ShowImage(resultForWebImage, 2);
IsShowingResult=false;
}
}
}
}
此时,我们只需要在适当的时候,启动这个任务。当用户点击send按钮时,启动服务: [java code]
[java] view plaincopyprint?
- sendBtnListener=new OnClickListener() {
-
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
-
- if(myBitmap!=null){
- mClientForWeb.compressByteImage(myBitmap, 75);;
- ServerTask task = new ServerTask();
- Log.e("msg","new ServerTask finished!");
- task.execute(Environment.getExternalStorageDirectory().toString() +mClientForWeb.INPUT_IMG_FILENAME);
- }
- }
- };
sendBtnListener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(myBitmap!=null){
mClientForWeb.compressByteImage(myBitmap, 75);;
ServerTask task = new ServerTask();
Log.e("msg","new ServerTask finished!");
task.execute(Environment.getExternalStorageDirectory().toString() +mClientForWeb.INPUT_IMG_FILENAME);
}
}
};
服务器端的PHP代码和matlab代码 1.服务器端的执行流程 首先,PHP获取到客户端发送过来的数据,然后在PHP中调用matlab代码对数据进行处理。PHP将处理后的结果再发回给客户端 2.本次试验 本次试验我们将实现图像的DCT变换。也即是:对客户端发送的图像做DCT变换,然后将变换后的图像发回给客户端。 如下图: [图]
3.matlab的执行方式 我们用命令行的方式执行matlab代码,参见[3]。在PHP中,我们只需要用exec函数调用相应的指令即可。 4.PHP代码 [php code]
[php] view plaincopyprint?
|
|