简体   繁体   English

不使用相机应用程序拍照-Android

[英]Taking picture without using camera application - Android

I understand how to capture an image using intents and launching the camera app using intents, however I would like to know how to do so in the following steps: 我了解如何使用Intent捕获图像并使用Intent启动相机应用程序,但是我想在以下步骤中了解如何做到这一点:

  1. display surfaceview showing preview of camera to user 显示surfaceview,向用户显示摄像机预览
  2. when user presses capture, display captured image on screen to user and hide surfaceview with camera preview (exact same behaviour as snapchat). 当用户按下捕获时,将捕获的图像显示在屏幕上给用户使用,并通过相机预览隐藏表面视图(行为与手套相同)。

NP - I do not want the camera app to be launched at anytime during this process, I want it all to be done within my own app. NP-我不希望在此过程中随时启动相机应用程序,我希望所有这些操作都在自己的应用程序中完成。 The problem with my current code is that it launches the camera app when the capture button is pressed. 我当前代码的问题在于,当按下“捕获”按钮时,它将启动相机应用程序。 Also, it does not display the taken photo properly, a white screen is shown instead. 另外,它无法正确显示所拍摄的照片,而是显示白色屏幕。 I currently have this code created: 我目前已创建此代码:

ANDROID ACTIVITY: 机器人活动:

public class CameraScreen extends Activity {
private Camera mCamera = null;
private SessionManager session;
private String rand_img;
private ImageView preview_pic;
private CameraPreview mCameraView = null;
static final int CAM_REQUEST = 1;
private RandomString randomString = new RandomString(10);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_screen);
    session = new SessionManager(getApplicationContext());
    try{
        mCamera = Camera.open();//you can use open(int) to use different cameras
    } catch (Exception e){
        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
    }


    if(mCamera != null) {
        mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);//add the SurfaceView to the layout
    }

    //btn to close the application
    Button imgClose = (Button)findViewById(R.id.imgClose);
    imgClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            System.exit(0);
        }
    });
    //btn to logout
    Button logout = (Button)findViewById(R.id.imgOpen);
    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            session.logOut();
            Intent a = new Intent(CameraScreen.this, MainActivity.class);
            startActivity(a);
            finish();
        }
    });
    //CAPTURE BUTTON
    Button snap = (Button) findViewById(R.id.snap);
    snap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent capture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            capture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile()));
            startActivityForResult(capture,CAM_REQUEST);
        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    if (mCamera != null) {
        mCamera.setPreviewCallback(null);
        mCameraView.getHolder().removeCallback(mCameraView);
        mCamera.release();
    }
}
@Override
public void onResume() {
    super.onResume();

    // Get the Camera instance as the activity achieves full user focus
    if (mCamera == null) {
        initializeCamera(); // Local method to handle camera initialization
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String path = "sdcard/city_life_pic/" + rand_img;
    preview_pic = (ImageView) findViewById(R.id.picturedisplay);
    FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
    camera_view.setVisibility(View.GONE);
    preview_pic.setVisibility(View.VISIBLE);
    preview_pic.setImageDrawable(Drawable.createFromPath(path));
}

protected void initializeCamera(){
    // Get an instance of Camera Object
    try{
        mCamera = Camera.open();//you can use open(int) to use different cameras
    } catch (Exception e){
        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
    }


    if(mCamera != null) {
        mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data
        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);//add the SurfaceView to the layout
    }
 }
private File getFile() {
    File folder = new File("sdcard/city_life_pic");
    if (!folder.exists()) {
        folder.mkdir();
    }
    rand_img = randomString.nextString() + ".jpg";
    File image = new File(folder,rand_img);
    return image;
}

}

CAMERA CLASS: 相机类别:

public class CameraPreview extends SurfaceView implements  SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera){
    super(context);

    mCamera = camera;
    mCamera.setDisplayOrientation(90);
    //get the holder and set this class as the callback, so we can get camera data here
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);;
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try{
        //when the surface is created, we can set the camera to draw images in this surfaceholder
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    //before changing the application orientation, you need to stop the preview, rotate and then start it again
    if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data
        return;

    try{
        mCamera.stopPreview();
    } catch (Exception e){
        //this will happen when you are trying the camera if it's not running
    }

    //now, recreate the camera preview
    try{
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    //our app has only one screen, so we'll destroy the camera in the surface
    //if you are unsing with more screens, please move this code your activity
    mCamera.stopPreview();
    mCamera.release();
}
}

You are opening the device's camera app by using this code 您正在使用此代码打开设备的相机应用程序

        Intent a = new Intent(CameraScreen.this, MainActivity.class);
        startActivity(a);
        finish();

Instead, to take a picture using your custom camera, use Camera#takePicture method instead 相反,要使用自定义相机拍摄照片,请改用Camera#takePicture方法

That would make your code 那会让你的代码

snap.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mCamera.takePicture(....)       //set parameters based on what you need
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM