30

我需要捕獲圖像所需的屏幕的一部分如何在Android中從自定義CameraView捕獲圖像?

捕獲圖像相機

那時其他屏幕內容,因爲它是

enter image description here

這怎麼可能

+0

您是使用相機意圖還是應用程序實現您自己的相機預覽表面? – Derzu 2013-03-16 16:44:06

+0

你想在全屏顯示預覽並只保存標有「相機視圖」的區域?或者您希望預覽僅在標有「相機視圖」的區域顯示? – Derzu 2013-03-16 16:51:59

+5

我想要預覽僅在標有「相機視圖」的區域顯示。我用Camera Intent。 – 2013-03-16 17:38:27

回答

24

嘗試使用表面觀創建動態相機視圖,並在您需要的部分設置。

下面的代碼嘗試

變量設置類級別(全球)在的onCreate

Button btn_capture; 
Camera camera1; 
SurfaceView surfaceView; 
SurfaceHolder surfaceHolder; 
public static boolean previewing = false; 

下面的代碼()方法

getWindow().setFormat(PixelFormat.UNKNOWN); 
    surfaceView = new SurfaceView(this); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
btn_capture = (Button) findViewById(R.id.button1); 

surfaceView.setBackgroundResource(R.drawable.your_background_image); 

if(!previewing){ 

     camera1 = Camera.open(); 
     if (camera1 != null){ 
      try { 
       camera1.setDisplayOrientation(90); 
       camera1.setPreviewDisplay(surfaceHolder); 
       camera1.startPreview(); 
       previewing = true; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

btn_capture.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      if(camera != null) 
      { 
       camera1.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG); 

      } 
     } 
    }); 

下面的代碼放在的onCreate後( ) in 你的班級

ShutterCallback myShutterCallback = new ShutterCallback(){ 

    public void onShutter() { 
     // TODO Auto-generated method stub 
    }}; 

PictureCallback myPictureCallback_RAW = new PictureCallback(){ 

    public void onPictureTaken(byte[] arg0, Camera arg1) { 
     // TODO Auto-generated method stub 
    }}; 

PictureCallback myPictureCallback_JPG = new PictureCallback(){ 

    public void onPictureTaken(byte[] arg0, Camera arg1) { 
     // TODO Auto-generated method stub 
     Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); 

     Bitmap correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true); 

    }}; 

public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    // TODO Auto-generated method stub 
    if(previewing){ 
     camera1.stopPreview(); 
     previewing = false; 
    } 

    if (camera1 != null){ 
     try { 
      camera1.setPreviewDisplay(surfaceHolder); 
      camera1.startPreview(); 
      previewing = true; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

public void surfaceCreated(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 

} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 

     camera1.stopPreview(); 
     camera1.release(); 
     camera1 = null; 
     previewing = false; 

} 

AndroidManifest.xml中用戶權限

<uses-permission android:name="android.permission.CAMERA"/> 
<uses-feature android:name="android.hardware.camera" android:required="false"/> 
<uses-feature android:name="android.hardware.camera.front" android:required="false"/> 

並且也不忘記(實現SurfaceHolder.Callback)到類。

+1

+1完美無瑕! – 2014-06-25 11:17:23

+0

適用於我;) – Reshma 2015-12-15 13:29:52

1

如果屏幕的一部分實際上是一個視圖,則只能捕獲此視圖。像這樣:

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
view.draw(canvas); 

如果你只想捕獲視圖的一小部分,你必須計算這邊的矩形。然後:

Bitmap bitmap = Bitmap.createBitmap(rect.width(),rect.height(),Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
canvas.save(); 
canvas.translate(-rect.left,-rect.top); 
view.draw(canvas); 
canvas.restore(); 

這只是一個僞代碼,但我希望你明白了。只需翻譯和繪製你需要的部分。

+5

我認爲你知道這個@Zielony,但你不能完全理解我的要求。請告訴我如何只在我的全屏幕中設置這部分相機?並從中捕捉圖像。之後,位圖和畫布用於繪製和存儲圖像供其他用途。 – 2013-03-13 17:05:33

+0

如果您只需要從照相機捕捉的照片的一部分,您可以簡單地裁剪圖像。捕捉圖片:http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity,然後用x,y,寬度,高度參數使用Bitmap.createBitmap裁剪它。 – Zielony 2013-03-13 17:09:19

+5

no..no ..我不需要裁剪圖像,但我需要當我打開相機,它不是全屏打開,但只在設置的屏幕部分打開。同上圖一樣。 – 2013-03-13 17:13:49

1

我使用CameraPreview在ApiDemos應用和編輯爲您的要求。

首先,將Preview類的代碼複製到同一個包中的新類文件中,以便它是公共的,並且可以在xml佈局文件中聲明它。記得添加如下多一個的構造:與樣品的寬度和高度

public Preview(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mSurfaceView = new SurfaceView(context); 
    addView(mSurfaceView); 

    // Install a SurfaceHolder.Callback so we get notified when the 
    // underlying surface is created and destroyed. 
    mHolder = mSurfaceView.getHolder(); 
    mHolder.addCallback(this); 
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

樣品佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Abow"/> 

    <com.example.android.apis.graphics.Preview 
     android:id="@+id/camera_view" 
     android:layout_width="240dp" 
     android:layout_height="180dp"> 
    </com.example.android.apis.graphics.Preview>  

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Below"/> 

</LinearLayout> 

在CameraPreview活動的onCreate()方法中,更改的setContentView部分如下:

setContentView(R.layout.camera_layout); 
mPreview = (Preview) findViewById(R.id.camera_view); 
1

使用TextureView預覽,設置layout_width和layout_height什麼都可以。 這裏是代碼:

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener { 
    private Camera mCamera; 
    private TextureView mTextureView; 

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

     mTextureView = (TextureView) findViewById(R.id.textureView); 
     mTextureView.setSurfaceTextureListener(this); 
    } 

    @Override 
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) { 
     mCamera = Camera.open(); 

     try { 
      mCamera.setPreviewTexture(surfaceTexture); 
      mCamera.setDisplayOrientation(90); 
      mCamera.startPreview(); 
     } catch (IOException exception) { 

     } 
    } 

    @Override 
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) { 
     //To change body of implemented methods use File | Settings | File Templates. 
    } 

    @Override 
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { 
     mCamera.startPreview(); 
     mCamera.release(); 
     return true; 
    } 

    @Override 
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { 
     //To change body of implemented methods use File | Settings | File Templates. 
    } 
} 

和xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <TextureView 
     android:layout_gravity="center" 
     android:id="@+id/textureView" 
     android:layout_width="200dp" 
     android:layout_height="300dp"/> 
</LinearLayout> 
+1

值得注意的是'TextureView'直到ICS(API等級14)才被引入。 – 2013-05-25 01:57:27

3

我已經喜歡那種相機所創建的。我所做的是,我用相機覆蓋了相機的其他區域,剪下圖像的中心部分並將其保存爲png文件,以使中心透明。

您將設置該圖像的背景圖像(相機預覽)。因此,它看起來像相機只是那個透明的部分或圓圈。

我用這個教程打開,創建預覽,並採取從相機設備 http://developer.android.com/guide/topics/media/camera.html

圖像在該部分(U可以在鏈路看到這一點,我提供上文)

private PictureCallback mPicture = new PictureCallback() { 

@Override 
public void onPictureTaken(byte[] data, Camera camera) { 
    //this is where you crop your image 
    BitmapFactory.Options opt = new BitmapFactory.Options(); 
    opt.inMutable = true; 
    Bitmap bitmap = BitmapFactory 
      .decodeByteArray(data, 0, data.length, opt); 

    bitmap=Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
    Canvas mcanvas=new Canvas(bitmap); 
    //do the cropping here, bitmap is the image you will use to crop 

    } 

} 

遵循此教程如何將圖像裁剪成圓形 Cropping circular area from bitmap in Android

+0

但是,如果要檢測到臉部並自動捕捉臉部,此解決方案將無法工作,因爲它可能不在圓形區域 – Neha 2017-10-30 01:21:56

相關問題