【问题标题】:Taking picture without using camera application - Android不使用相机应用程序拍照 - Android
【发布时间】:2016-02-18 05:40:16
【问题描述】:

我了解如何使用 Intent 捕获图像并使用 Intent 启动相机应用,但我想知道如何通过以下步骤执行此操作:

  1. 向用户显示显示相机预览的表面视图
  2. 当用户按下捕获时,在屏幕上向用户显示捕获的图像并使用相机预览隐藏表面视图(与 snapchat 完全相同的行为)。

NP - 我不希望在此过程中随时启动相机应用程序,我希望这一切都在我自己的应用程序中完成。我当前代码的问题是它在按下捕获按钮时启动相机应用程序。此外,它无法正确显示拍摄的照片,而是显示白屏。我目前已创建此代码:

安卓活动:

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;
}

}

相机类:

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();
}
}

【问题讨论】:

    标签: java android camera


    【解决方案1】:

    您正在使用此代码打开设备的相机应用

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

    如果要使用自定义相机拍照,请改用Camera#takePicture 方法

    这将使您的代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多