微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何捕获图像并将其保存在sd卡中

如何解决如何捕获图像并将其保存在sd卡中

| 大家好,我正在使用以下代码从我的应用程序访问摄像头。 该应用程序能够访问相机,我还添加一个按钮,其onclicklistener添加了以下代码行:-
camera.takePicture(mShutterCallback,mPictureCallbackRaw,mPictureCallbackJpeg);
现在我不知道会发生什么,但是应用程序卡住了,我不得不强制关闭它,然后我什至无法访问本机相机应用程序。 我认为它离开了应用程序而没有释放Camera对象。 另外,图像永远不会保存。 代码:-
SurfaceHolder holder;
SurfaceView  surface;
Camera camera;
Boolean isPreviewRunning,fromOnResume;
//Preview mpreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    isPreviewRunning = false;
    fromOnResume = false;
   // requestwindowFeature(Window.FEATURE_NO_TITLE);
    surface = (SurfaceView)findViewById(R.id.surface);
    holder = surface.getHolder();
  holder.addCallback(this);
  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  Button btn = (Button)findViewById(R.id.click);
    btn.setonClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // Todo Auto-generated method stub
            camera.takePicture(mShutterCallback,mPictureCallbackJpeg);
            //onCreate(null);


        }

    });

}


@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
/*  if (isPreviewRunning) {  
          camera.stopPreview();  
         }  */

    Camera.Parameters parameters = camera.getParameters();

    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size size = sizes.get(0);
    parameters.setPreviewSize(size.width,size.height);

    camera.setParameters(parameters);
     camera.startPreview();  
     isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // Todo Auto-generated method stub

     camera = Camera.open();

     try {
        camera.setPreviewdisplay(holder);
    } catch (IOException e) {
        // Todo Auto-generated catch block
        e.printstacktrace();

    }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // Todo Auto-generated method stub

    camera.stopPreview();
    camera.release();
    camera=null;
}

 Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data,Camera c) {
  Log.e(getClass().getSimpleName(),\"PICTURE CALLBACK RAW: \" + data);
  camera.startPreview();
 }
 };

 Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data,\"PICTURE CALLBACK JPEG: data.length = \" + data);

  camera.startPreview();
 }
 };


 Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
 public void onShutter() {
 Log.e(getClass().getSimpleName(),\"SHUTTER CALLBACK\");
 }
 };
/*
 *  protected void onRestoreInstanceState(Bundle savedInstanceState)
 {

 super.onRestoreInstanceState(savedInstanceState);
 }
 protected void onResume()
 {

 Log.e(getClass().getSimpleName(),\"onResume\");
 camera.open();
 fromOnResume=true;
 super.onResume();
 }

 protected void onSaveInstanceState(Bundle outState)
 {
 super.onSaveInstanceState(outState);
 }

 @Override
protected void onPause() {
    // Todo Auto-generated method stub
     camera.release();
    super.onPause();
}


 protected void onStop()
 {
 Log.e(getClass().getSimpleName(),\"onStop\");
 super.onStop();
 }*/
    

解决方法

我知道这并不完全是您的问题的答案,但是使用库存相机应用程序会不会更容易?您可以在活动中使用以下代码访问它:
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.capture);

    Button capture = (Button) findViewById(R.id.capture_button);
    capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // We use the stock camera app to take a photo
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,getImageUri());
            startActivityForResult(intent,TAKE_PHOTO_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Uri imagePath = getImageUri();

        doSomething();
    }
}

/**
 * Get the uri of the captured file
 * @return A Uri which path is the path of an image file,stored on the dcim folder
 */
private Uri getImageUri() {
    // Store image in dcim
    File file = new File(Environment.getExternalStorageDirectory() + \"/DCIM\",CAPTURE_TITLE);
    Uri imgUri = Uri.fromFile(file);

    return imgUri;
}
    ,只是建议,将jpg上载EXIF,然后再将其上载到服务器。由于当今智能手机相机的质量,我发现上传图像的速度非常缓慢。一个简单的解决方案是使用exif阅读程序提取jpeg的缩略图,将其保存为新的jpeg,然后上传。它与原始照片相同,但较小(小于100kb)。我不确定是否要获得图像质量,但如果不想要,并且要上传大量图片,请使用exif方法。我在python sl4a上编程并使用EXIF.py,但我确定Java中也存在类似情况。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。