设为首页 收藏本站
查看: 1414|回复: 0

基于web的android图像处理示例(Win7+Apache+PHP+Matlab+Android)【转】

[复制链接]

尚未签到

发表于 2015-5-18 11:56:39 | 显示全部楼层 |阅读模式
  本文将介绍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变换)
   DSC0000.jpg
  实验平台: 服务器: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变换,然后将变换后的图像发回给客户端。 如下图: [图]
DSC0001.jpg
  3.matlab的执行方式 我们用命令行的方式执行matlab代码,参见[3]。在PHP中,我们只需要用exec函数调用相应的指令即可。 4.PHP代码 [php code]
  






[php] view plaincopyprint?


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-68044-1-1.html 上篇帖子: win7旗舰版系统设置自动锁屏问题 下篇帖子: 保护眼睛的屏幕设置 && Win2008R2中的Win7桌面效果设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表