android相機預覽

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   ar   

android訪問相機使用的是Camera.open 來返回一個Camera對象,設定好顯示的視圖後,調用Camera的預覽功能函數 startPreview,停止預覽函數是 stopPreview!

activity_mail.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.ssln.camera.MainActivity" >    <VideoView        android:id="@+id/videoView1"        android:layout_width="match_parent"        android:layout_height="300dp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="開啟" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="關閉" />    </LinearLayout></LinearLayout>

MainActivity.java

package com.ssln.camera;import java.io.IOException;import android.app.Activity;import android.hardware.Camera;import android.os.Bundle;import android.view.SurfaceHolder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.VideoView;public class MainActivity extends Activity implements OnClickListener {    private Camera myCamra; // 相機    private VideoView video; // 顯示    private Button btnOpen, btnClose;    private boolean isPreview=false;    //是否預覽    private SurfaceHolder holder;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        video = (VideoView) findViewById(R.id.videoView1);        btnOpen = (Button) findViewById(R.id.button1);        btnClose = (Button) findViewById(R.id.button2);        btnOpen.setOnClickListener(this);        btnClose.setOnClickListener(this);        holder=video.getHolder();    }    @Override    public void onClick(View v) {        if (v == btnOpen) {            initCamera();        } else if (v == btnClose) {            if(myCamra!=null && isPreview){                myCamra.stopPreview();//停止預覽                myCamra.release();      //釋放資源                myCamra=null;                        isPreview=false;            }        }    }    /**     * 初始化相機     */    private void initCamera(){        if(!isPreview)        {            myCamra=Camera.open();    //開啟相機裝置        }        if(myCamra!=null && !isPreview)        {            try {                myCamra.setPreviewDisplay(holder);                myCamra.startPreview();//開始預覽            } catch (IOException e) {                e.printStackTrace();            }            isPreview=true;        }            }}

訪問相機是需要許可權的,記得加上

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.ssln.camera"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <!-- 相機存取權限 -->    <uses-permission android:name="android.permission.CAMERA"/>

<!-- 加下下面兩句,否則會出現 Fail to connect to camera service. 錯誤 -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />


<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>

效果預覽

 

android相機預覽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.