Android—實現自訂相機倒計時拍照,android自訂

來源:互聯網
上載者:User

Android—實現自訂相機倒計時拍照,android自訂

這篇部落格為大家介紹Android自訂相機,並且實現倒計時拍照功能

首先自訂拍照會用到SurfaceView控制項顯示照片的預覽地區,以下是布局檔案:

兩個TextView是用來顯示提示資訊和倒計時的秒數的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#266194"    android:orientation="vertical"    tools:context=".TestActivity" >    <SurfaceView        android:id="@+id/surfaceView"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:orientation="vertical" >        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="請調整位置到此地區"            android:textColor="#ff0000"            android:textSize="32sp" />        <TextView            android:id="@+id/tv_time"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:paddingTop="10dp"            android:gravity="center_horizontal"            android:textColor="#266194"            android:textSize="32sp" />    </LinearLayout></RelativeLayout>

接下來是mainActivity中的具體實現以及詳細注釋:

package com.dhsr.pujiejia.ui;import java.io.File;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.Date;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.res.Configuration;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.PixelFormat;import android.hardware.Camera;import android.hardware.Camera.CameraInfo;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.Window;import android.view.WindowManager;import android.widget.TextView;import com.example.pujiejiaapp.R;@SuppressLint({ "NewApi", "SdCardPath" })public class CameraActivity extends Activity implements Runnable {    // 預覽圖片範圍    private SurfaceView surfaceView;    private TextView tv_time;    // 倒計時拍攝    private int cameratime = 4;    private Camera camera;    private boolean preview = false;    // 檔案名稱字    private String filename;    // 檔案名稱字的帶的時間戳記    private String timeString;    // 格式化時間    private SimpleDateFormat dateFormat;    // 日期對象    private Date date;    // 控制線程    boolean stopThread = false;    private File file;    String photo;    private Handler mHandler = new Handler() {        public void handleMessage(android.os.Message msg) {            int what = msg.what;            switch (what) {            case 222:                tv_time.setText("" + cameratime);                if ("0".equals(tv_time.getText().toString())) {                    tv_time.setText("拍攝成功!");                    takePhoto();                }                break;            }        };    };    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test);        CameraActivity.this.setFinishOnTouchOutside(false);        // 初始化資料        findView();        surfaceView.getHolder().addCallback(new SufaceListener());        /* 下面設定Surface不維護自己的緩衝區,而是等待螢幕的渲染引擎將內容推送到使用者面前 */        surfaceView.getHolder()                .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        surfaceView.getHolder().setFixedSize(200, 200); // 設定解析度    }    @Override    protected void onStart() {        // TODO Auto-generated method stub        super.onStart();        // 開啟線程        new Thread(this).start();    }    private final class SufaceListener implements SurfaceHolder.Callback {        /**         * surface改變         */        @Override        public void surfaceChanged(SurfaceHolder holder, int format, int width,                int height) {        }        /**         * surface建立         */        @Override        public void surfaceCreated(SurfaceHolder holder) {            try {                for (int i = 0; i < Camera.getNumberOfCameras(); i++) {                    CameraInfo info = new CameraInfo();                    Camera.getCameraInfo(i, info);                    // 調用系統的自拍                    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {                        camera = Camera.open(i);                    }                }                Camera.Parameters parameters = camera.getParameters();                /* 每秒從網路攝影機捕獲5幀畫面, */                parameters.setPreviewFrameRate(5);                /* 設定照片的輸出格式:jpg */                parameters.setPictureFormat(PixelFormat.JPEG);                /* 照片品質 */                parameters.set("jpeg-quality", 85);                WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);                camera.setParameters(parameters);                camera.setPreviewDisplay(surfaceView.getHolder());// 通過SurfaceView顯示取景畫面                camera.startPreview();                preview = true;            } catch (Exception e) {            }        }        /**         * surface銷毀         */        @Override        public void surfaceDestroyed(SurfaceHolder holder) {            if (camera != null) {                if (preview)                    camera.stopPreview();                camera.release();                camera = null;            }        }    }    /**     * 拍攝照片     */    private void takePhoto() {        // 執行拍照效果        camera.takePicture(null, null, new Camera.PictureCallback() {            @Override            public void onPictureTaken(byte[] data, Camera camera) {                try {                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,                            data.length);                    timeString = formatDate();                    //儲存到data/data目錄自訂檔案夾下                     filename = "/data/data/com.example.pujiejiaapp/images/"                     + timeString + ".jpg";                    File file = new File(filename);                    boolean createNewFile = file.createNewFile()                    System.out.println("建立檔案夾成功沒有" + createNewFile);                    System.out.println(file);                    FileOutputStream outStream = new FileOutputStream(file);                    bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outStream);                    outStream.flush();                    outStream.close();                    // 重新瀏覽                    camera.stopPreview();                    camera.startPreview();                    preview = true;                } catch (Exception e) {                    e.printStackTrace();                } finally {                }            }        });    }    @Override    public void run() {        while (!stopThread) {            try {                //按秒數倒計時                Thread.sleep(1000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            cameratime--;            mHandler.sendEmptyMessage(222);            if (cameratime <= 0) {                break;            }        }    }    // 初始化資料    private void findView() {        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);        tv_time = (TextView) findViewById(R.id.tv_time);    }    // 格式化系統的時間    public String formatDate() {        date = new Date(System.currentTimeMillis());        // 日期格式        dateFormat = new SimpleDateFormat("'IMG'_yyyyMMddHHmmss");        return dateFormat.format(date);    }    @Override    protected void onDestroy() {        // TODO Auto-generated method stub        // 線程已關閉        super.onDestroy();        stopThread = true;    }}

核心代碼詳解:

1.建立SurfaceView時,surfaceCreated()方法中

for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
                    CameraInfo info = new CameraInfo();
                    Camera.getCameraInfo(i, info);
                    // 調用系統的自拍
                    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                        camera = Camera.open(i);
                    }
                }

此部分代碼為開啟相機時預設開啟自拍CameraInfo.CAMERA_FACING_BACK為預設開啟後置網路攝影機,CameraInfo.CAMERA_FACING_FRONT自拍

2.照片拍攝takePhoto()方法中:

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
                            data.length);
                    timeString = formatDate();
                     filename = "/data/data/com.example.pujiejiaapp/images/"
                     + timeString + ".jpg";
                    photo = timeString + ".jpg";
                    File file = new File(filename);
                    boolean createNewFile = file.createNewFile();
                    FileOutputStream outStream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outStream);

此部分代碼為將拍攝到的圖片儲存為以bitmap格式儲存在指定的目錄下

3.開子線程用於倒計時拍攝

public void run() {
        while (!stopThread) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cameratime--;
            mHandler.sendEmptyMessage(222);
            if (cameratime <= 0) {
                break;
            }
        }
    }

希望大家理解核心代碼的詳細注釋,歡迎提供意見,希望能給大家帶來協助,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.