拍照(二)得到全尺寸圖片,並進行壓縮,拍照尺寸

來源:互聯網
上載者:User

拍照(二)得到全尺寸圖片,並進行壓縮,拍照尺寸

<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/take_photo"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Take Photo" />    <Button        android:id="@+id/get_photo"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="get Photo" />    <ImageView        android:id="@+id/picture"        android:layout_width="300dp"        android:layout_height="300dp"        android:layout_gravity="center_horizontal" /></LinearLayout>

 

package com.example.choosepictest;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;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.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity implements OnClickListener {    static final int REQUEST_IMAGE_CAPTURE = 1;    private Button takePhoto;    private Button getPhoto;    private ImageView picture;    private Uri imgUri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        takePhoto = (Button) findViewById(R.id.take_photo);        getPhoto = (Button) findViewById(R.id.get_photo);        picture = (ImageView) findViewById(R.id.picture);        takePhoto.setOnClickListener(this);        getPhoto.setOnClickListener(this);    }        @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.take_photo:            dispatchTakePictureIntent();            break;                default:            break;        }    }        // 儲存全尺寸照片    String mCurrentPhotoPath;    private void dispatchTakePictureIntent() {                File appDir = new File(Environment.getExternalStorageDirectory(),                "/etoury/picCache");        if (!appDir.exists()) {         appDir.mkdirs();        }        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")                .format(new Date());        String fileName = timeStamp + ".jpg";                File outputImage = new File(appDir, fileName);        try {            if (outputImage.exists()) {                outputImage.delete();            }            outputImage.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        mCurrentPhotoPath = outputImage.getAbsolutePath();        imgUri = Uri.fromFile(outputImage);        // 意圖 相機        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");        intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);        // 如果有相機        if (intent.resolveActivity(getPackageManager()) != null) {            startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);        }    }        //解碼縮放圖片(Decode a Scaled Image)    private void setPic() {        // Get the dimensions of the View        int targetW = picture.getWidth();        int targetH = picture.getHeight();        // Get the dimensions of the bitmap        BitmapFactory.Options bmOptions = new BitmapFactory.Options();        // 該 值設為true那麼將不返回實際的bitmap,也不給其分配記憶體空間這樣就避免記憶體溢出了。但是允許我們查詢圖片的資訊這其中就包括圖片大小資訊        bmOptions.inJustDecodeBounds = true;        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);        int photoW = bmOptions.outWidth;        int photoH = bmOptions.outHeight;        // Determine how much to scale down the image        // Math.min求最小值        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);        // Decode the image file into a Bitmap sized to fill the View        bmOptions.inJustDecodeBounds = false;        // 設定恰當的inSampleSize可以使BitmapFactory分配更少的空間以消除該錯誤        bmOptions.inSampleSize = scaleFactor;        // 如果inPurgeable設為True的話表示使用BitmapFactory建立的Bitmap,用於儲存Pixel的記憶體空間在系統記憶體不足時可以被回收        bmOptions.inPurgeable = true;        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);        picture.setImageBitmap(bitmap);    }        @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESULT_OK) {            switch (requestCode) {            case REQUEST_IMAGE_CAPTURE:            galleryAddPic();                setPic();                break;            default:                break;            }        }    }
    //發送一條通知添加照片到相簿
    private void galleryAddPic() {
        
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        mediaScanIntent.setData(imgUri);
        this.sendBroadcast(mediaScanIntent);
        }}

聯繫我們

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