Android application: how to use the camera and grab the image bytes?

孤街醉人 提交于 2019-12-24 00:03:50

问题


I'm trying to create a small app for Android that takes a picture using the device's camera and put's a PNG frame on top of it. This way the final saved picture will have a beach on top of it, or hats, or anything. Does anyone have any sample programs with this behavior?


回答1:


Have a look at the SDK documentation on using the image capture intent here.

I start my image capture intent like this:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is a private member in my activity:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

Then get the byte array back from the camera by using the following onActivityResult handler:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {        
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            Bitmap bmp = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            AddImage(byteArray);

        } else if (resultCode == Activity.RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }               
}

After that you can do all the processing you want on the image.



来源:https://stackoverflow.com/questions/9027695/android-application-how-to-use-the-camera-and-grab-the-image-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!