android中的多媒體應用camera

來源:互聯網
上載者:User

MainActivity.java

 

代碼

package com.amaker.ch11.app;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity {
// 聲明拍照介面組件SurfaceView
private SurfaceView surfaceView;
// 聲明介面控制組件SurfaceHolder
private SurfaceHolder surfaceHolder;
// 聲明照相機
private Camera camera;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 執行個體化拍照介面組件
surfaceView = (SurfaceView)findViewById(R.id.preview);
// 從SurfaceView中獲得SurfaceHolder
surfaceHolder = surfaceView.getHolder();
// 為SurfaceHolder 添加回調
surfaceHolder.addCallback(surfaceCallback);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

// 響應按鍵事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_CAMERA||keyCode==KeyEvent.KEYCODE_SEARCH){
takePic();
return true;
}
return super.onKeyDown(keyCode, event);
}
// 拍照方法
private void takePic(){
// 停止預覽
camera.stopPreview();
// 拍照
camera.takePicture(null,null, pictureCallback);
}
// 照片回調
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
new SavePictureTask().execute(data);
camera.startPreview();
}
};

// 儲存照片任務類
class SavePictureTask extends AsyncTask<byte[],String,String>{
@Override
protected String doInBackground(byte[]... params) {
// 建立檔案
File picture = new File(Environment.getExternalStorageDirectory(),"picture.jpg");
// 如果檔案存在刪除掉
if(picture.exists())picture.delete();
try {
// 獲得檔案輸出資料流
FileOutputStream fos = new FileOutputStream(picture.getPath());
// 寫到該檔案
fos.write(params[0]);
// 關閉檔案流
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
// SurfaceHodler 回調,處理開啟相機,關閉相機以及照片尺寸的改變
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 開啟相機
camera = Camera.open();
try {
// 設定預覽
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// 獲得相機參數
Camera.Parameters parameters = camera.getParameters();
// 設定照片大小
parameters.setPreviewSize(width, height);
// 設定照片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 設定相機參數
camera.setParameters(parameters);
// 開始預覽
camera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 停止預覽
camera.stopPreview();
// 釋放相機資源
camera.release();
camera = null;
}
};
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/preview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
</android.view.SurfaceView>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amaker.ch11.app"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <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>
    <uses-sdk android:minSdkVersion="3" />

</manifest>

相關文章

聯繫我們

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