如何在没有从服务或线程预览的情况下进行相机捕捉?

Posted

技术标签:

【中文标题】如何在没有从服务或线程预览的情况下进行相机捕捉?【英文标题】:how to take camera capture without a preview from a service or thread? 【发布时间】:2012-03-16 21:33:44 【问题描述】:

是否可以在不显示相机预览的情况下捕获图像?,我有一个要求,我应该能够从线程或服务中捕获图像,而不会干扰前台应用程序,而我不想这样做显示相机预览,但我仍然想在后台捕获图像并将其存储在设备中

【问题讨论】:

您还看到过这些其他问题吗? ***.com/questions/2386025/…***.com/questions/7398897/… 任何使用前置摄像头的示例程序,无需预览和保存 sd 卡 【参考方案1】:

试试这个代码。活动启动时,它将单击图片。它有一个预览,但预览大小设置为 0 x 0 dp。

package fortyonepost.com.pwop;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;

public class TakePicture extends Activity implements SurfaceHolder.Callback

    //a variable to store a reference to the Image View at the main.xml file
    private ImageView iv_image;
    //a variable to store a reference to the Surface View at the main.xml file
    private SurfaceView sv;

    //a bitmap to display the captured image
    private Bitmap bmp;

    //Camera variables
    //a surface holder
    private SurfaceHolder sHolder;
    //a variable to control the camera
    private Camera mCamera;
    //the camera parameters
    private Parameters parameters;

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

        //get the Image View at the main.xml file
        iv_image = (ImageView) findViewById(R.id.imageView);

        //get the Surface View at the main.xml file
        sv = (SurfaceView) findViewById(R.id.surfaceView);

        //Get a surface
        sHolder = sv.getHolder();

        //add the callback interface methods defined below as the Surface View callbacks
        sHolder.addCallback(this);

        //tells Android that this surface will have its data constantly replaced
        sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
    
         //get camera parameters
         parameters = mCamera.getParameters();

         //set camera parameters
         mCamera.setParameters(parameters);
         mCamera.startPreview();

         //sets what code should be executed after the picture is taken
         Camera.PictureCallback mCall = new Camera.PictureCallback()
         
             @Override
             public void onPictureTaken(byte[] data, Camera camera)
             
                 //decode the data obtained by the camera into a Bitmap
                 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                 //set the iv_image
                 iv_image.setImageBitmap(bmp);
             
         ;

         mCamera.takePicture(null, null, mCall);
    

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    
        // The Surface has been created, acquire the camera and tell it where
        // to draw the preview.
        mCamera = Camera.open();
        try 
           mCamera.setPreviewDisplay(holder);

         catch (IOException exception) 
            mCamera.release();
            mCamera = null;
        
    

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    
        //stop the preview
        mCamera.stopPreview();
        //release the camera
        mCamera.release();
        //unbind the camera from this object
        mCamera = null;
    

参考:http://www.41post.com/3794/programming/android-take-a-picture-without-displaying-a-preview

【讨论】:

这是在正确的轨道上,但您使用的是 Activity 而不是后台线程或服务。 这拍了一张照片,但是旋转了90度!为了检查,我将表面视图设为 200x200 像素而不是 0 宽度。表面视图也会横向显示图像。【参考方案2】:

我无法在我的主要活动中获得可接受的工作答案(当我将表面视图更改为 1dpi 到 1dpi 时,它作为一个单独的活动工作,但在拍照后,我的动态视图会消失)。

为了在我的主要活动中拍照而不预览,我使用了来自Android - Take picture without preview 的代码(修改版本如下)。

...

private SurfaceView sv;
Camera myCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

...

private void takePhoto() 

        Camera.Parameters parameters = myCamera.getParameters();
        // myCamera.setDisplayOrientation(180);
        // parameters.setPictureFormat(ImageFormat.JPEG);
        // parameters.setPictureFormat(PixelFormat.JPEG);
        myCamera.setParameters(parameters);
        SurfaceView mview = sv;
        try 
            myCamera.setPreviewDisplay(mview.getHolder());
            myCamera.startPreview();
            myCamera.takePicture(null, null, photoCallback);
         catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

    Camera.PictureCallback photoCallback=new Camera.PictureCallback() 
        public void onPictureTaken(byte[] data, Camera camera) 

            saveMyPhotoToParse(data);
            // Log.i(DEBUG_TAG,"image pseudo saved");
        
    ;

【讨论】:

这一行出现空指针异常 myCamera.setPreviewDisplay(mview.getHolder());【参考方案3】:

在另一个线性布局中进行预览并更改 alpha 值 255 全透明 0 全不透明

cameraPreview_holderLL.setAlpha(255);// 启用预览

cameraPreview_holderLL.setAlpha(0);//禁用预览

<LinearLayout
     android:id="@+id/camera_preview_holder"
    android:layout_
    android:layout_
    android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/camera_preview"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>
</LinearLayout>

【讨论】:

【参考方案4】:

你可以使用一个虚拟的surfaceholder.callback

private SurfaceHolder.Callback dummySurfaceCallback = new SurfaceHolder.Callback()


    @Override
    public void surfaceCreated(SurfaceHolder holder)
    
        // do nothing
    

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    
        // do nothing
    

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    
        // do nothing
    
;

并将其应用于surfaceholder(previewHolder)

    preview = (SurfaceView) findViewById(R.id.preview);
    previewHolder = preview.getHolder();
    previewHolder.addCallback(dummySurfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

然后使用previewholder在mediarecorder中设置previewdisplay。

    mediaRecorder.setPreviewDisplay(previewHolder.getSurface());

此方法将为您提供黑屏预览。 这对我有用,我希望这会有所帮助......

【讨论】:

以上是关于如何在没有从服务或线程预览的情况下进行相机捕捉?的主要内容,如果未能解决你的问题,请参考以下文章

倒置相机预览字节数组[重复]

在android中使用没有预览或表面的相机

无需预览即可捕获视频

Android:如何在不拍照的情况下从相机捕获文本?

如何显示视频捕捉滤镜对话框(相机设置,如亮度,曝光)?

使用 FFmpeg 进行 Android 相机捕捉