我正在研究一种分析ECG图的图像处理应用程序。为此,我需要检测图表的某些峰值。
如何在用户界面中打印“坐标”,“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 :(得分:0)
您的应用程序崩溃是因为您尝试从工作线程访问Views
,这在Android中是禁止的。我建议使用AsyncTask
类,它在工作线程上运行某个任务,并提供在UI线程上运行的方法,您可以在其中更新Views
。希望这会有所帮助。
答案 1 :(得分: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 + " " );
}
}