2012-03-16 66 views
23

是否可以在不顯示相機預覽的情況下捕捉圖像?我有一個要求,即我應該能夠從線程或服務中捕捉圖像,而不會干擾前景應用程序,而我不想顯示相機預覽,但我仍然想要在後臺捕獲圖像並將其存儲在設備中如何在沒有服務或線程預覽的情況下拍攝相機?

+2

你見過這些其他問題嗎? http://stackoverflow.com/questions/2386025/android-camera-without-preview http://stackoverflow.com/questions/7398897/how-to-use-camera-to-take-picture-in-a-background -service-on-android – 2012-03-16 21:44:08

+0

任何示例程序使用前置攝像頭,無需預覽並保存SD卡 – city0666 2012-03-16 21:52:28

回答

12

嘗試此代碼。當活動啓動時,它會點擊一張圖片。它具有預覽,但預覽大小設置爲0 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

+0

這是在正確的軌道上,但您使用的是Activity而不是後臺線程或服務。 – Sam 2014-11-22 23:20:02

+0

這需要一張照片,但它旋轉了90度!爲了檢查,我製作了200x200像素的表面視圖,而不是0寬度。表面視圖還會顯示圖像的側面。 – Raja 2017-02-17 18:26:19

1

你可以使用一個虛擬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在mediarecoreder中設置previewdisplay。

mediaRecorder.setPreviewDisplay(previewHolder.getSurface()); 

這個方法會讓你有黑屏預覽。 這對我有用,我希望這會對我有幫助...

3

我無法接受在我的主要活動中工作的答案(當我將表面視圖更改爲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"); 
     } 
    }; 
+1

此行的空指針異常myCamera.setPreviewDisplay(mview.getHolder()); – 2016-05-08 11:07:19

1

使內的另一個線性佈局預覽並改變α值255完全透明0充分不透明

cameraPreview_holderLL.setAlpha( 255); //啓用預覽

cameraPreview_holderLL.setAlpha(0 ); //禁用預覽

<LinearLayout 
    android:id="@+id/camera_preview_holder" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
     <LinearLayout 
      android:id="@+id/camera_preview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" > 
     </LinearLayout> 
</LinearLayout> 
相關問題