How to capture the photo from camera on Android Emulator?

天大地大妈咪最大 提交于 2019-12-07 04:51:22

问题


Based on this article, I'm trying do capture the photo from camera on Android Emulator. I followed the instructions as per they said. But I didn't get the positive result.

I'm getting the Player is null, while I'm running the WebcamBroadcaster.java(Java Application).

Is anyone achieve this before? If yes, Just let me how to do.

Or

Is there any other option to capture the photo from camera on Android Emulator?


回答1:


In Android emulator 2.1 my code is working to capture image but not working in others version of android

To start camera for capture you can start camera for capture using below intent filter

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);

After capturing you will get the image as bitmap so you need to get activity result

if (resultCode == RESULT_OK && requestCode == CAMERA_RESULT) {
    Bundle extras = data.getExtras();
    if(extras.containsKey("data")) {
        Bitmap bmp = (Bitmap) extras.get("data");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] image = baos.toByteArray();
        if(image != null) {
            //User this byte array in your application
        }
    }else {
        Toast.makeText(getBaseContext(), "Fail to capture Image", Toast.LENGTH_LONG).show();
    }
}

Edit:

Now almost in all the emulators this code is working.




回答2:


As hes mentioning in his article he wrote this code in haste and it may be kinda buggy therefore. It's not said to work everywhere at all.

Im assuming your using exactly this code to run this thing:

CameraSource cs = new SocketCamera("192.168.0.100", 9889, 320, 240, true);
if (!cs.open()) { /* deal with failure to obtain camera */ }
while(/*some condition*/) {
  cs.capture(canvas) //capture the frame onto the canvas
}
cs.close();

What is, by the way, the main purpose of doing such things? All camera aligned things should tested exhaustively on a real device, because it can cause loads of problems which does not occur at an emulator. The camera implementation of the camera is for debug/testing purposes, only!

I would strongly recommend to not spend to much time into getting this running, it won't lead you very far. It still has not been tested on a real device, though, which would be the very most important.

I hope I didn't disappoint you too much with this answer :/



来源:https://stackoverflow.com/questions/8505647/how-to-capture-the-photo-from-camera-on-android-emulator

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