我怎样才能让我的Andr​​oid应用程序采取从相机照片,而不用preVIEW? [英] How can I enable my android app to take pictures from the camera without preview?

查看:135
本文介绍了我怎样才能让我的Andr​​oid应用程序采取从相机照片,而不用preVIEW?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我要的是一个应用程序,只是拍摄照片从相机启动时。没有布局,没有摄像头preVIEW ......没什么,只是一个简单的应用程序图标来启动应用程序。一旦启动了相机应该拍照。

Now all I want is an app that just takes the picture from the camera upon launching. No layout, no camera preview...nothing, just a simple app icon to launch the app. Once launched the camera should take the picture.

下面是preVIEW图像,即我不希望出现在我的应用程序。我只是想eanble我的应用程序的点击拍摄照片按钮包围在图像中这里:

Here is the preview image, that i do not want to appear in my app. I just want to eanble my app to click the take picture button encircled in the image here:

在拍摄照片然后我需要保存在我的照片库。

Once the picture is taken then I need to save it in my photo gallery.

有人可以指导我在这里?谢谢

Can someone guide me here? Thanks

下面是我曾尝试在仿真器和设备上的某些code。

Here is some code that I have tried on emulator and device.

import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Main extends Activity implements SurfaceHolder.Callback{

    private Camera camera;
     private ImageButton cameraClick;
     private SurfaceHolder mHolder; 

    /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
      mHolder = surfaceView.getHolder();
      mHolder.addCallback(this);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  

      cameraClick = (ImageButton) findViewById(R.id.cameraClick);
      cameraClick.setOnClickListener( new OnClickListener() {
       public void onClick(View v)
       {
            camera.takePicture(shutterCallback, rawCallback,jpegCallback);
       }
      });  





      }

     // Handles when shutter open
     ShutterCallback shutterCallback = new ShutterCallback()
     {
      public void onShutter()
      {   

      }
     };

     /** Handles data for raw picture */
     PictureCallback rawCallback = new PictureCallback()
     {
      public void onPictureTaken(byte[] data, Camera camera)
      {   

      }
     }; 

     /** Handles data for jpeg picture */
     PictureCallback jpegCallback = new PictureCallback()
    {
      public void onPictureTaken(byte[] data, Camera camera)
    {
       // we do something using return byte[] of taken image.
      }
     };

     @Override
     public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)
    {

      // Set camera preview size,orientation,rotation using parameters 

    Camera.Parameters parameters = camera.getParameters();
            parameters.set("orientation", "portrait");
            camera.setParameters(parameters);
             camera.startPreview();       

     }

     @Override
     public void surfaceCreated(SurfaceHolder holder)
    {
      camera = Camera.open();
             try {
        camera.setPreviewDisplay(holder);
       }
    catch (IOException e)
    {
        e.printStackTrace();
       }

     }

     @Override
     public void surfaceDestroyed(SurfaceHolder holder)
    {
      camera.stopPreview();
             camera.release();
            camera = null; 

     }

}

现在我能听到画面上点击按钮,单击这两个在模拟器和设备。现在我想保存的图片为好。任何线索?

Now I am able to hear the picture click on the button click both on the emulator and the device. Now I want to save the picture as well. Any clue?

推荐答案

使用低于code。经测试的为我工作。任何问题,请随时提出意见。

Use below code. Its Tested and Worked for me. Any issues please feel free to put comments.

private void takeSnapShots()
{
      Toast.makeText(getApplicationContext(), "Image snapshot   Started",Toast.LENGTH_SHORT).show();
     // here below "this" is activity context.  
 SurfaceView surface = new SurfaceView(this);
    Camera camera = Camera.open();
    try {
        camera.setPreviewDisplay(surface.getHolder());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    camera.startPreview();
    camera.takePicture(null,null,jpegCallback);
}


 /** picture call back */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) 
{
    FileOutputStream outStream = null;
    try {
  String dir_path = "";// set your directory path here
        outStream = new FileOutputStream(dir_path+File.separator+image_name+no_pics+".jpg");    
        outStream.write(data);
        outStream.close();
        Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally 
    {
        camera.stopPreview();
        camera.release();
        camera = null;
        Toast.makeText(getApplicationContext(), "Image snapshot Done",Toast.LENGTH_LONG).show();


    }
    Log.d(TAG, "onPictureTaken - jpeg");
}
};

这篇关于我怎样才能让我的Andr​​oid应用程序采取从相机照片,而不用preVIEW?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆