Android調(diào)用系統(tǒng)攝像頭拍照并剪裁壓縮

由于業(yè)務(wù)需要寫(xiě)了一個(gè)Android手機(jī)拍照的功能Demo,同時(shí)實(shí)現(xiàn)了圖片剪裁和圖片壓縮。以下是源碼

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、印江ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的印江網(wǎng)站制作公司

package com.klp.demo_025;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

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.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView iv;
    private Button button;
    private File file;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.p_w_picpathView1);
        button = (Button) findViewById(R.id.button1);

        file = new File(this.getExternalFilesDir(null), "p_w_picpath.jpg");
        uri = Uri.fromFile(file);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(intent, 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 == 2) {
                startPhotoZoom(uri);
            } else {
                try {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(),
                            options);
                    // 壓縮圖片
//                    bitmap = compressImage(bitmap,500);

                    if (bitmap != null) {
                        // 顯示圖片
                        iv.setImageBitmap(bitmap);
                        // 保存圖片
                        FileOutputStream fos = null;
                        fos = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                    }
                } catch (Exception e) {
                    // TODO: handle exception

                }
            }

        }

    }

    /**
     * 裁剪圖片
     * 
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "p_w_picpath/*");
        intent.putExtra("crop", "true");// crop=true 有這句才能出來(lái)最后的裁剪頁(yè)面.
        intent.putExtra("aspectX", 1);// 這兩項(xiàng)為裁剪框的比例.
        intent.putExtra("aspectY", 1);// x:y=1:1
        intent.putExtra("outputX", 200);//圖片輸出大小
        intent.putExtra("outputY", 200);
        intent.putExtra("output", uri);
        intent.putExtra("outputFormat", "JPEG");// 返回格式
        startActivityForResult(intent, 3);
    }

    /**
     * 將圖片p_w_picpath壓縮成大小為 size的圖片(size表示圖片大小,單位是KB)
     * 
     * @param p_w_picpath
     *            圖片資源
     * @param size
     *            圖片大小
     * @return Bitmap
     */
    private Bitmap compressImage(Bitmap p_w_picpath, int size) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
        p_w_picpath.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100;
        // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮
        while (baos.toByteArray().length / 1024 > size) {
            // 重置baos即清空baos
            baos.reset();
            // 每次都減少10
            options -= 10;
            // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
            p_w_picpath.compress(Bitmap.CompressFormat.JPEG, options, baos);

        }
        // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        // 把ByteArrayInputStream數(shù)據(jù)生成圖片
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
        return bitmap;
    }

}

下面是布局,很簡(jiǎn)單

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/p_w_picpathView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="拍照" />

</RelativeLayout>

最后注意,一定要在AndroidManifest.xml添加SD卡訪問(wèn)權(quán)限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

網(wǎng)站欄目:Android調(diào)用系統(tǒng)攝像頭拍照并剪裁壓縮
本文路徑:http://muchs.cn/article8/johgop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、、定制開(kāi)發(fā)、關(guān)鍵詞優(yōu)化網(wǎng)站設(shè)計(jì)網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)