1. 程式人生 > >安卓自定義相機,使用系統相機教程原始碼都有

安卓自定義相機,使用系統相機教程原始碼都有

使用系統相機

Android中使用系統相機是很方便的,單這僅僅是簡單的使用而已,並不能獲得什麼特殊的效果。

要想讓應用有相機的action,咱們就必須在清單檔案中做一些宣告,好讓系統知道,如下

 <intent-filter>
                <action android:name="android.intent.action.IMAGE_CAPTURE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter
>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

action的作用就是宣告action的型別,便於Intent的使用,category的作用就是註冊,沒有它。相關操作將不起作用。 
一種方式是簡單粗暴的實現,如下

Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQ_1);
//然後在 onActivityResult方法中實現資料的獲取,此處是展示在了一個ImageView上
if(resultCode==RESULT_OK){
            if(requestCode==REQ_1){
                Bundle bundle=data.getExtras();
                Bitmap bitmap=(Bitmap) bundle.get
("data"); imageView.setImageBitmap(bitmap); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

小總結:這樣的好處是簡單快捷,但是在現在的android智慧機中,好多相片都是很大的,這裡獲得的僅僅是一個縮圖罷了

另外一種方式是稍微溫婉一點了,而且效果也更好一點,好處就在於它是先將照片資訊儲存到本地一個臨時檔案中,然後讓ImageView去相關路徑下進行讀取,這樣就可以獲得清晰度很高的圖片了。如下

/*
     * 此方法的存在意義就是不在onActivityResult方法的data中獲取我們拍照的縮圖,而是從我們的檔案輸出目錄下直接檢視原圖
     * 這樣的好處就是可以對大容量的照片進行便捷的準確的操作
     */
public void onStartCarema2(View view){ Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //見你給你路徑傳遞迴需要的處理方法中 Uri uri=Uri.fromFile(new File(myFilePath)); //設定檔案的輸出路徑 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, REQ_2); } //然後在onActivityResult方法中進行相關的處理就可以了 else if(requestCode==REQ_2){ FileInputStream fis=null; try { fis=new FileInputStream(myFilePath); Bitmap bitmap=BitmapFactory.decodeStream(fis); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //記得最後一定要關閉相關的流操作。否則會引起相關的異常的。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

開發自定義的相機

由於開發自定義的相機要進行相關的許可權的生命,所以一定不要忘記在清單檔案中做相關的處理,如下

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
  • 1
  • 2
  • 1
  • 2

然後有以下幾個步驟:

  1. 建立Camera,並完成初始化Camera,開始預覽,釋放資源三個方法
  2. 與Activity的SurfaceView進行繫結。
  3. 在系統的onPause(),onResume()方法中進行相關狀態設定
  4. 對Camera進行引數設定,作用就是對照片型別和狀態進行相關的設定
  5. 將拍得的照片進行展示,一般會新開一個Activity,用ImageView進行承載,我們還可以在此Activity上新增TextView,實現水印效果等其他的美化操作
  6. 另外,如果想加入自動聚焦的功能,就可以在SurfaceView上新增onClickListener(),對螢幕進行偵聽,呼叫myCamera.autoFocus(null);方法即可

以上就是整個思路

接下來就是使用系統Camera的程式碼展示

(可以直接copy相關程式碼塊,新增到你的應用中去,實現Camera這一功能。)

首先是MainActivity

  • 佈局
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/startCarema"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="StartCarema"
        android:onClick="onStartCarema"
        />
    <Button 
        android:id="@+id/startCarema2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="StartCarema2"
        android:onClick="onStartCarema2"
        />
    <Button 
        android:id="@+id/customCarema"
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="CustomCarema"
        android:onClick="onCustomCarema"
        />

    <ImageView 
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 程式碼
package com.example.camerademo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    //為下面的獲取請求所用
    private static int REQ_1=1;
    private static int REQ_2=2;

    Button btn_startCareme,btn_startCarema2,btn_customCarema;
    ImageView imageView;

    //定義照片儲存的路徑
    private String myFilePath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_startCareme=(Button) findViewById(R.id.startCarema);
        btn_startCarema2=(Button) findViewById(R.id.startCarema2);
        btn_customCarema=(Button) findViewById(R.id.customCarema);
        imageView=(ImageView) findViewById(R.id.imageview);

        //初始化不同手機的SD卡的路徑
        myFilePath=Environment.getExternalStorageDirectory().getPath();
        myFilePath=myFilePath+"/"+"temperature.png";
    }

    public void onCustomCarema(View view){
        Intent intent=new Intent(this,CustomCarema.class);
        startActivity(intent);
    }

    public void onStartCarema(View view){
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQ_1);
    }

    /*
     * 此方法的存在意義就是不在onActivityResult方法的data中獲取我們拍照的縮圖,而是從我們的檔案輸出目錄下直接檢視原圖
     * 這樣的好處就是可以對大容量的照片進行便捷的準確的操作
     */
    public void onStartCarema2(View view){
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //見你給你路徑傳遞迴需要的處理方法中
        Uri uri=Uri.fromFile(new File(myFilePath));
        //設定檔案的輸出路徑
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, REQ_2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            if(requestCode==REQ_1){
                Bundle bundle=data.getExtras();
                Bitmap bitmap=(Bitmap) bundle.get("data");
                imageView.setImageBitmap(bitmap);
            }else if(requestCode==REQ_2){
                FileInputStream fis=null;
                try {
                    fis=new FileInputStream(myFilePath);
                    Bitmap bitmap=BitmapFactory.decodeStream(fis);
                    imageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

接下來是自定義相機的程式碼

  • 主介面佈局
<?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" >

    <Button 
        android:id="@+id/capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Capture"
        android:onClick="onCapture"
        />

    <SurfaceView 
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />



</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • ResultActivity的佈局
<?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="match_parent"
        android:layout_height="wrap_content"
        android:text="Capture Result"
        android:textSize="28dp"
        android:textColor="#BFAACD"
        android:gravity="center"
        />    
        <ImageView 
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            />

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 程式碼 
    首先是CustomCamera類,
package com.example.camerademo;

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

import android.app.Activity;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

@SuppressWarnings("unused")
public class CustomCarema extends Activity implements SurfaceHolder.Callback{

    private Camera myCamera;

    private SurfaceView preview;
    private SurfaceHolder myHolder;   //myHolder勇於展現surfaceView的影象

    private Camera.PictureCallback myPictureCallBack=new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera arg1) {
            //將拍照得到的資料資訊儲存到本地
            File tempFile=new File("/sdcard/temp.png");
            try {
                FileOutputStream fos=new FileOutputStream(tempFile);
                fos.write(data);
                fos.close();
                //然後將這個照片的資料資訊傳送給要進行展示的Activity即可
                Intent intent=new Intent(CustomCarema.this,ResultActivity.class);
                intent.putExtra("PicturePath", tempFile.getAbsolutePath());
                startActivity(intent);
                //拍照結束之後銷燬當前的Activity,進入到圖片展示介面
                CustomCarema.this.finish();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customcarema);
        preview=(SurfaceView) findViewById(R.id.preview);
        myHolder=preview.getHolder();
        myHolder.addCallback(this);

        //實現點選螢幕自動聚焦的功能,此處並不需要拍照,故只是聚焦
        preview.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                myCamera.autoFocus(null);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(myCamera==null){
            myCamera=getCamera();
            if(myHolder != null ){
                setStartPreview(myCamera, myHolder);
            }
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        releaseCamera();
    }

    /**
     * 釋放相機的資源
     */
    private void releaseCamera(){
        if(myCamera !=null ){
            myCamera.setPreviewCallback(null);
            myCamera.stopPreview();
            myCamera.release();
            myCamera=null;
        }
    }


    /**
     * 拍照的一些引數設定,點選此按鈕之後會觸發拍照的會掉,進而實現拍照的效果
     * @param view
     */
    public void onCapture(View view){
        Camera.Parameters parameters=myCamera.getParameters();
        //設定照片的型別
        parameters.setPictureFormat(ImageFormat.JPEG);
        parameters.setPictureSize(800, 600);
        //設定為自動聚焦
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        //設定為自動聚焦是不夠的,因為我們先得到的是最為清晰的圖片,所以要在聚焦成功的時候才進行拍照
        myCamera.autoFocus(new Camera.AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                // TODO Auto-generated method stub
                if(success){
                    myCamera.takePicture(null, null, myPictureCallBack);
                }
            }
        });
    }

    /**
     * 獲取系統的一個Camera物件
     */
    private Camera getCamera(){
        Camera camera=null;
        try{
            camera=Camera.open();
        }catch(Exception e){
            e.printStackTrace();
        }

        return camera;
    }

    /**
     * 開始預覽相機的內容,其實就是講surfaceHolder與之繫結
     */
    private void setStartPreview(Camera camera,SurfaceHolder holder){
        //直接呼叫系統方式繫結預覽
        try {
            camera.setPreviewDisplay(holder);
            //由於系統預設使用橫屏預覽,,所以要進行設定
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }




    @Override
    public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        myCamera.stopPreview();
        setStartPreview(myCamera, myHolder);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        setStartPreview(myCamera, myHolder);
    }


    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        releaseCamera();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187

然後是結果介面程式碼:

package com.example.camerademo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class ResultActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultactivity);
        String path=getIntent().getStringExtra("PicturePath");
        ImageView imageview=(ImageView) findViewById(R.id.picture);
        //由於這樣稚嫩獲得橫屏,所以我們要使用流的形式來轉換
//      Bitmap bitmap=BitmapFactory.decodeFile(path);
//      imageview.setImageBitmap(bitmap);
        FileInputStream fis;
        try {
            fis = new FileInputStream(path);
            Bitmap bitmap=BitmapFactory.decodeStream(fis);
            Matrix matrix=new Matrix();
            matrix.setRotate(90);
            bitmap=Bitmap.createBitmap(bitmap, 0,0, bitmap.getWidth()
                    ,bitmap.getHeight(),matrix,true);
            imageview.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

以上程式碼中已經做了下關的程式碼註釋

總結:

安卓6.0以上版本記得加動態許可權,不然會報空指標哦,還有點選拍照事件那裡  別忘了加進去,不然拍照沒反應

//寫這篇部落格的目的一方面是為了今後複習時方便一點,畢竟思路在此刻是清晰的,過幾天就有可能忘記了。另一方面若能幫到在這方面有欠缺的小夥伴們的話,那是更好的了。最後尤其要感謝的是Imooc中eclipse_xu 老師,我就是看著他的視訊