L
Lisa2
Neues Mitglied
- 0
Hallo allerseits!
Ich muss ein Bild duch Sobel filtern. Ich habe zuerst die fonctionalität auf einen JavaApplet schon eingesetzt und es lief einwandfrei. Allerdings muss das Filter auf einem Android-Mobiltelefon funktionieren.
Überrachung : Android kennt weder Image noch PixelGrabber weder Toolkit noch MemoryImageSource
wie kann ich bitte meine methode "getSobelOf" unter Android zum Laufen bringen?
z.b.
Originale Methode(Die vom Compiler erzeugte Fehler sind kommentiert):
Hier ist die Solel-Klasse, das ist nur Mathe wird von Android natürlich akzeptiert
Vielen Dank für Eure Hilfe!
Ich muss ein Bild duch Sobel filtern. Ich habe zuerst die fonctionalität auf einen JavaApplet schon eingesetzt und es lief einwandfrei. Allerdings muss das Filter auf einem Android-Mobiltelefon funktionieren.
Überrachung : Android kennt weder Image noch PixelGrabber weder Toolkit noch MemoryImageSource
wie kann ich bitte meine methode "getSobelOf" unter Android zum Laufen bringen?
z.b.
Code:
public Bitmap getSobelOf(Bitmap bitmap) {
//*****
return sobelBitmap;
}
Code:
public Image getSobelOf(Image image) { [COLOR=DarkOrange][I]//ERROR:Image cannot be resolved to a type[/I][/COLOR]
int width = image.getWidth(null);
int height = image.getHeight(null);
int orig[] = new int[width * height];
PixelGrabber grabber = new PixelGrabber(image, 0, 0, width,
height, orig, 0, width);[COLOR=DarkOrange][I]//ERROR: PixelGrabber cannot be resolved to a type[/I][/COLOR]
try {
grabber.grabPixels();
} catch (InterruptedException e2) {
System.out.println("error: " + e2);
}
sobel sobelObject = new sobel();
sobelObject.init(orig, width, height);
orig = sobelObject.process();
Toolkit tk = Toolkit.getDefaultToolkit();[COLOR=DarkOrange][I]//ERROR:Toolkit cannot be resolved to a type[/I][/COLOR]
Image sobelImage = tk.createImage(new MemoryImageSource(width, height, orig, 0,
width));[COLOR=DarkOrange][I]//ERROR:MemoryImageSource cannot be resolved to a type[/I][/COLOR]
return sobelImage;
}
Code:
import java.lang.Math;
public class sobel {
int[] input;
int[] output;
float[] template = {
-1, 0, 1, -2, 0, 2, -1, 0, 1
};;
int progress;
int templateSize = 3;
int width;
int height;
double[] direction;
public void sobel() {
progress = 0;
}
public void init(int[] original, int widthIn, int heightIn) {
width = widthIn;
height = heightIn;
input = new int[width * height];
output = new int[width * height];
direction = new double[width * height];
input = original;
}
public int[] process() {
float[] GY = new float[width * height];
float[] GX = new float[width * height];
int[] total = new int[width * height];
progress = 0;
int sum = 0;
int max = 0;
for (int x = (templateSize - 1) / 2; x < width - (templateSize + 1) / 2; x++) {
progress++;
for (int y = (templateSize - 1) / 2; y < height - (templateSize + 1) / 2; y++) {
sum = 0;
for (int x1 = 0; x1 < templateSize; x1++) {
for (int y1 = 0; y1 < templateSize; y1++) {
int x2 = (x - (templateSize - 1) / 2 + x1);
int y2 = (y - (templateSize - 1) / 2 + y1);
float value = (input[y2 * width + x2] & 0xff)
* (template[y1 * templateSize + x1]);
sum += value;
}
}
GY[y * width + x] = sum;
for (int x1 = 0; x1 < templateSize; x1++) {
for (int y1 = 0; y1 < templateSize; y1++) {
int x2 = (x - (templateSize - 1) / 2 + x1);
int y2 = (y - (templateSize - 1) / 2 + y1);
float value = (input[y2 * width + x2] & 0xff)
* (template[x1 * templateSize + y1]);
sum += value;
}
}
GX[y * width + x] = sum;
}
}
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
total[y * width + x] = (int) Math.sqrt(GX[y * width + x] * GX[y * width + x]
+ GY[y * width + x] * GY[y * width + x]);
direction[y * width + x] = Math.atan2(GX[y * width + x], GY[y * width + x]);
if (max < total[y * width + x])
max = total[y * width + x];
}
}
float ratio = (float) max / 255;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
sum = (int) (total[y * width + x] / ratio);
output[y * width + x] = 0xff000000 | ((int) sum << 16 | (int) sum << 8 | (int) sum);
}
}
progress = width;
return output;
}
public double[] getDirection() {
return direction;
}
public int getProgress() {
return progress;
}
}