我试图做的,是用相机意图拍照,检索和转换称照片为灰度字节数组(注:我没有兴趣在得到灰度图像,只需要字节的数据)。于是最后,应用门槛和平均所有像素超过阈值。
What I am trying to do, is to take a photo using a camera intent, retrieve and convert said photo to a grayscale byte array (note: I am not interested in getting a grayscale image, just need the byte data). Then finally, apply a threshold and average all the pixels above the threshold.
的code相关的片段是:
The relevant snippet of code is:
@Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
InputStream stream = null;
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
try {
stream = getContentResolver().openInputStream(data.getData());
bmp = BitmapFactory.decodeStream(stream);
bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
for(int x = 0; x < bmp.getWidth(); ++x) {
for(int y = 0; y < bmp.getHeight(); ++y) {
int index = y * bmp.getWidth() + x;
int R = (pixels[index] >> 16) & 0xff;
int G = (pixels[index] >> 8) & 0xff;
int B = (pixels[index]) & 0xff;
double Grey = (0.299 * R + 0.587 * G + 0.114 * B);
if(Grey > 20) {
sum += Grey;
count++;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
double Y = sum / count;
敬酒的评论是没有进行测试,我使用的早期,以确保意图是工作 - 它是,但它给的路径是
The toast comment is there for testing, I used that earlier to make sure the intent was working - it was, but the path it gave was
内容://媒体/外部/图片/媒体/ ##
Content://media/external/images/media/##
(其中##为下一张照片编号)。
(where ## is the next photo number).
我已经在Eclipse模拟器尝试这样做,我也得到一个RuntimeException错误的位图的开始。我得到了类似的崩溃时,我做的LG擎天柱L3现场测试(Android版本2.3.6)。
I have tried this in the Eclipse emulator, and I get a RuntimeException error at where the bitmap starts. I get a similar crash when I do a live test on an LG Optimus L3 (Android version 2.3.6).
我相信我在的code中的位图部分疯玩起来的地方(是的,我已经阅读了开发人员指南和多个线程在这里和其他地方)。这是怎么回事错了位图的一部分?
I am convinced I have goofed up somewhere in the bitmap part of the code (and yes, I have read the developers guide and several threads here and in other places). What is going wrong with the bitmap part?
在一些调查遍及这里的一些问题(和我那些是特别有用upvoted),并在各种编码的地方,相当多的迟发夜间自编码教育,我现在的工作。下面是工作code片断:
After a bit of research throughout some questions here (and I upvoted the ones that were especially useful), and in various coding places and quite a bit of late-night-self-coding-education, I have it working now. Below is the working code snippet:
@Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATE_TAKEN
};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
if ( c1.moveToFirst() ) {
String uristringpic = "content://media/external/images/media/" +c1.getInt(0);
Uri uri = Uri.parse(uristringpic);
try {
Bitmap bm = android.provider.MediaStore.Images.Media.getBitmap(cr, uri);
int w = bm.getWidth();
int h = bm.getHeight();
Bitmap bmg = Bitmap.createBitmap(w, h, bm.getConfig());
for(int x = 0; x < w; ++x) {
for(int y = 0; y < h; ++y) {
int pixel = bm.getPixel(x, y);
a = Color.alpha(pixel);
r = Color.red(pixel);
g = Color.green(pixel);
b = Color.blue(pixel);
r = g = b = (int)(0.299 * r + 0.587 * g + 0.114 * b);
bmg.setPixel(x, y, Color.argb(a, r, g, b));
grey = 0.299 * r + 0.587 * g + 0.114 * b;
if(grey > 20) {
sum += grey;
count++;
}
//int grey = (r + g + b) / 3;
}
}
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
imageView.setImageBitmap(bmg);
//Toast.makeText(MainActivity.this, String.valueOf(Y), Toast.LENGTH_LONG).show();
//Toast.makeText(this, uri.toString(),Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
}
//Toast.makeText(this, "newuri " + uri, Toast.LENGTH_LONG).show();
} c1.close();
//Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
double Y = sum / count;