2013-02-22 90 views
0

我正在研究分析ECG图的图像处理应用程序。为此,我需要检测图的某些峰。从Android中的数组打印坐标

如何在用户界面中打印“坐标”,“xcoor”和“ycoor”?我试着敬酒,但它不起作用。我尝试了textView,但是应用程序的关闭。

package com.thesis.results; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import java.util.ArrayList; 
import android.widget.Toast; 


import com.example.edgedetection.R; 

public class MainActivity extends Activity { 

    // initialize variables 
    static int i = 0; 
    static int bl_ = 0; // number of black; pixels in the image 
    static int op_ = 0; 
    static int Al = 0; 
    static int Re = 0; 
    static int Gr = 0; 
    static int Bl = 0; 
    static int Alp = 0; 
    static int Red = 0; 
    static int Gre = 0; 
    static int Blu = 0; 
    static int stop = 0; 
    static int stopx = 0; 
    static int stopy = 1000; 
    static int xcoor[]; 
    static int ycoor[]; 
    static int width; 
    static int height; 
    static int RRdistance; 
    static double voltage; 
    static int peakcoordinates; 

    ImageView imageSource, imageAfter; 
    Bitmap bitmap_Source; 
    ProgressBar progressBar; 
    Button process; 
    TextView counter; 
    TextView coordinates; 
    private Handler handler; 
    Bitmap afterProcess; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     process = (Button) findViewById(R.id.btn_process); 
     imageSource = (ImageView) findViewById(R.id.imageSource); 
     imageAfter = (ImageView) findViewById(R.id.imageAfter); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     counter = (TextView) findViewById(R.id.counter); 
     coordinates = (TextView) findViewById(R.id.coordinates); 

     process.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       bitmap_Source = BitmapFactory.decodeResource(getResources(), 
         R.drawable.test_ideal_graph); 
       handler = new Handler(); 
       StratBackgroundProcess(); 
      } 
     }); 

    } 

    private void StratBackgroundProcess() { 

     Runnable runnable = new Runnable() { 

      public void run() { 
       afterProcess = processingBitmap(bitmap_Source, 0, 0, 0); 

       handler.post(new Runnable() { 

        public void run() { 
         progressBar.setVisibility(View.GONE); 
         imageAfter.setImageBitmap(afterProcess); 

         calculatevoltage(); 
         // calculateRRdistance(); 
         counter.setText("" + bl_ + "@ (" + stopx + "," + stopy 
           + " " + "and" + 
           ")" + " {" + width + "," + height + " } = " 
             + voltage + "mV" + " " + "R-R distance:" + " " 
             + RRdistance); 

         coordinates.setText(" " + xcoor + "," + ycoor + " "); 
        } 

        private void calculatevoltage() { 
         // TODO Auto-generated method stub 
         voltage = ((0.1 * height) * (height - stopy))/height; 
         // 1.5 mV is the total voltage of the graph, 1 box = 
         // 0.1mV 
        } 

        //private void calculateRRdistance() { 
        // TODO Auto-generated method stub 
        //  RRdistance = stopx1 - stopx; 
        // 1.5 mV is the total voltage of the graph, 1 box = 
        // 0.1mV 

        // } 

       }); 
      } 
     }; 
     new Thread(runnable).start(); 
    } 

    public static Bitmap processingBitmap(Bitmap src, double red, double green, 
      double blue) { 
     // image size 
     width = src.getWidth(); 
     height = src.getHeight(); 
     // create output bitmap 
     Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig()); 
     // color information 
     int A, R, G, B; 
     int pixel; 
     int flag = 0; 

     //array 
     int[] trial = new int[width]; 
     // scan through all pixels 

     for (int x = 0; x < width; ++x) { 

      flag = 0; 

      for (int y = 0; y < height; ++y) { 
       // get pixel color 
       pixel = src.getPixel(x, y); 
       // apply filtering on each channel R, G, B 
       Al = Color.alpha(pixel); 
       Re = (int) (Color.red(pixel)); 
       Gr = (int) (Color.green(pixel)); 
       Bl = (int) (Color.blue(pixel)); 
       // set new color pixel to output bitmap 

       if ((Re == 0) && (Gr == 0) && (Bl == 0) && (flag == 0)) { 
        bmOut.setPixel(x, y, Color.argb(255, 0, 0, 0)); 
        flag = 1; 
        trial[x] = y; 

       } else 
        bmOut.setPixel(x, y, Color.argb(255, 255, 255, 255)); 

      } 
     } 

     //detect all possible peaks 

     for (int x = 1; x < width; x++) { 
      if (trial[x] < trial[x - 1] && trial[x] < trial[x + 1]) { 
       peakcoordinates = src.getPixel(x, trial[x]); //get pixels, how to display? (textview, toast?) 
       //Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show(); 
      } 

      //detect all R peaks 
      for (int y = 1; y > (trial[1]-50); y++){ 

       xcoor[i] = x; 
       ycoor[i] = y; 
      } 
      return bmOut; 
     } 
     return bmOut; 
    } 
} 
+0

你能告诉我们更多的代码吗?这是活动中的代码吗? – faceman 2013-02-22 08:38:32

+0

嗨,我把我的整个Java代码上面。 :) – cookie23 2013-02-22 08:43:02

+0

当我添加行coordinates.setText(“”+ xcoor +“,”+ ycoor +“”);,应用程序无法启动。 – cookie23 2013-02-22 08:46:25

回答

0

您的应用程序崩溃是因为您尝试从工作线程访问您的Views,Android工程师禁止此工作线程。我建议使用AsyncTask类,它在工作线程上运行某个任务,并提供在UI线程上运行的方法,您可以在其中更新Views。希望这可以帮助。

+0

观看次数?对不起,但我对Android编程有点新鲜。我不熟悉Views或AsyncTask。 :( – cookie23 2013-02-22 08:53:44

+0

)如果你想更新你的textview或者显示一个toast消息或其他更新到UI,你不能从Runnable的run方法来完成。查看这个stackoverflow问题:[AsyncTask](http://stackoverflow.com/questions/9671546/asynctask-android-example) – faceman 2013-02-22 09:14:07

0

只能在UI线程中修改视图(类似TextView,ImageView)。试试AsyncTask

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     process = (Button) findViewById(R.id.btn_process); 
     imageSource = (ImageView) findViewById(R.id.imageSource); 
     imageAfter = (ImageView) findViewById(R.id.imageAfter); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     counter = (TextView) findViewById(R.id.counter); 
     coordinates = (TextView) findViewById(R.id.coordinates); 

     process.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
         // TODO Auto-generated method stub 
         bitmap_Source = BitmapFactory.decodeResource(getResources(), 
             R.drawable.test_ideal_graph); 
         handler = new Handler(); 
         new BackgroundTask().execute(); 
       } 
     }); 

} 

private class BackgroundTask extends AsyncTask<String, Long, String> { 

    @Override 
    protected String doInBackground(String... s) { 
     afterProcess = processingBitmap(bitmap_Source, 0, 0, 0); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     progressBar.setVisibility(View.GONE); 
     imageAfter.setImageBitmap(afterProcess); 

     calculatevoltage(); 
     // calculateRRdistance(); 
     counter.setText("..."); 
     coordinates.setText(" " + xcoor + "," + ycoor + " "); 
    } 
} 
+0

我尝试过这样做,但它仍然没有启动,必须​​在(String ... s)的位置吗? – cookie23 2013-02-22 09:23:27

+0

我从我的旧项目中复制过来,但你并不需要更改代码现在有什么错误信息? – iForests 2013-02-22 09:33:19

+0

它没有错误,但它无法启动。我不明白为什么,我越来越沮丧。:(( – cookie23 2013-02-22 09:35:51