如何捕获图像并将其保存到 SD 卡中

发布于 2024-11-01 09:57:47 字数 3463 浏览 5 评论 0原文

大家好,我正在使用以下代码从我的应用程序访问相机。 该应用程序能够访问相机,我还添加了一个按钮,其 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, mPictureCallbackRaw, 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, Camera c) {
  Log.e(getClass().getSimpleName(), "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();
 }*/

hey guys i am using the following code to access camera from my application.
The application is able to access the camera i have also added a button whose onclicklistener adds this line of code :-

camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);

Now i dont know what happens but the application gets stuck i have to force close it and then i can not even access the native camera application.
I think it leaves the application without releasing the Camera object.
Plus the image never gets saved.

Code :-

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, mPictureCallbackRaw, 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, Camera c) {
  Log.e(getClass().getSimpleName(), "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();
 }*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅 2024-11-08 09:57:47

我知道这并不完全是您问题的答案,但是使用库存相机应用程序不是更容易吗?您可以在活动中使用以下代码访问它:

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

I know this isn't exactly an answer to your question, but wouldn'nt it be easier to use the stock camera application? You can access it using this code in your activity:

@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;
}
许一世地老天荒 2024-11-08 09:57:47

只是建议,在将 jpg 上传到服务器之前对 jpg 使用 EXIF。我发现由于当今智能手机摄像头的质量,上传图像非常缓慢。一个简单的解决方案是使用 exif 读取程序提取 jpeg 的缩略图,将所述缩略图保存为新的 jpeg,然后上传。它与原始照片相同,但小得多(低于 100kb)。我不确定这是否是您想要的图像质量,但如果不是,并且要上传大量图片,请使用 exif 方法。我在 python sl4a 上编程并使用了 EXIF.py,但我确信 Java 中也有类似的东西。

Just a suggestion, use EXIF on jpg before uploading it to the server. I found uploading images tediously slow because of the quality of todays smartphone cameras. A simple solution is using an exif reading program to extract the thumbnail of the jpeg, save said thumbnail as a new jpeg, and upload that. It's identical to the original photo yet much smaller (under 100kb). I am not sure if it's image quality you want, but if not, and to upload lots of pics, use the exif method. I programmed on python sl4a and used EXIF.py, but I'm sure there's similar in Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文