溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android編程中圖片操作類定義與用法的示例分析

發(fā)布時(shí)間:2021-08-11 10:56:51 來源:億速云 閱讀:130 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章給大家分享的是有關(guān)Android編程中圖片操作類定義與用法的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

主界面類:拍照及選擇相冊圖片

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
 * Android中圖片操作(拍照,相冊圖片選擇及圖片裁剪)
 * 作者:ldm
 * 時(shí)間:20162016/7/11 09:09
 */
public class ImageTestActivity extends Activity implements View.OnClickListener {
  //拍照
  private Button take_photo;
  //從相冊中選擇圖片
  private Button local_pic;
  //圖片展示
  private ImageView upload_image;
  //定義操作常量
  private final static int TAKE_PHOTO_REQUEST = 1;
  private final static int LOCAL_PICS_REQUEST = 2;
  private final static int UPLOAD_PIC_REQUEST = 3;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_test);
    //初始化控件及監(jiān)聽事件
    initViews();
  }
  private void initViews() {
    this.upload_image = (ImageView) findViewById(R.id.upload_image);
    this.take_photo = (Button) findViewById(R.id.take_photo);
    this.local_pic = (Button) findViewById(R.id.local_pics);
    this.take_photo.setOnClickListener(this);
    this.local_pic.setOnClickListener(this);
  }
  @Override
  public void onClick(View view) {
    if (view.getId() == R.id.take_photo) {//拍照
      //調(diào)用系統(tǒng)拍照In
      Intent photoIn = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(photoIn, TAKE_PHOTO_REQUEST);
    } else if (view.getId() == R.id.local_pics) {//從相冊選擇
      Intent picsIn = new Intent(Intent.ACTION_GET_CONTENT);
      picsIn.setType("image/*");//設(shè)置選擇的數(shù)據(jù)類型為圖片類型
      startActivityForResult(picsIn, LOCAL_PICS_REQUEST);
    }
  }
  //拍照或選擇相冊后,數(shù)據(jù)在這里處理
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (null == data) {
      return;
    }
    switch (requestCode) {
      case TAKE_PHOTO_REQUEST:
        Bundle bundle = data.getExtras();//獲取到圖片數(shù)據(jù)
        if (null != bundle) {
          Bitmap bm = bundle.getParcelable("data");
          //把圖片展示在ImView上
//          upload_image.setImageBitmap(bm);
          //對圖片剪
          Uri uri = ImageUtils.saveBitmapToSdCard(bm);
          startImageCrop(uri);
        }
        break;
      case LOCAL_PICS_REQUEST:
        Uri uri = data.getData();//從圖片的Uri是以cotent://格式開頭的
        //獲取到圖片
        Bitmap bm = ImageUtils.uri2Bitmap(ImageTestActivity.this, uri);
        //把圖片展示在ImView上
//        upload_image.setImageBitmap(bm);
        //把拍照的圖片保存到本地并轉(zhuǎn)換成文件格式的Uri
        Uri fileUri = ImageUtils.saveBitmapToSdCard(bm);
        //對圖片剪
        startImageCrop(fileUri);
        break;
      case UPLOAD_PIC_REQUEST:
        //把裁剪后的圖片展示出來
        Bundle b = data.getExtras();
        Bitmap bitmap = b.getParcelable("data");
        //圖片展示出來
        upload_image.setImageBitmap(bitmap);
        break;
    }
  }
  /**
   * @param
   * @description 圖片裁剪
   * @author ldm
   * @time 2016/7/11 10:07
   */
  private void startImageCrop(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");//設(shè)置Uri及類型
    intent.putExtra("crop", "true");//
    intent.putExtra("aspectX", 2);//X方向上的比例
    intent.putExtra("aspectY", 1);//Y方向上的比例
    intent.putExtra("outputX", 200);//裁剪區(qū)的X方向?qū)?
    intent.putExtra("outputY", 100);//裁剪區(qū)的Y方向?qū)?
    intent.putExtra("scale", true);//是否保留比例
    intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
    intent.putExtra("return-data", true);//是否將數(shù)據(jù)保留在Bitmap中返回dataParcelable相應(yīng)的Bitmap數(shù)據(jù)
    startActivityForResult(intent, UPLOAD_PIC_REQUEST);
  }
}

布局文件

<?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/take_photo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="拍照上傳" />
  <Button
    android:id="@+id/local_pics"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="本地圖庫上傳" />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="圖片信息展示"
    android:layout_marginLeft="10dp"
    android:textSize="16sp"/>
  <ImageView
    android:id="@+id/upload_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>
</LinearLayout>

圖片操作工具類

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.format.DateFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Locale;
/**
 * description:從圖片中獲取到的Uri是以content://開頭的,從U中找到對應(yīng)圖片
 * 作者:ldm
 * 間:20162016/7/11 09:47
 */
public class ImageUtils {
  public static Bitmap uri2Bitmap(Context mContext, Uri uri) {
    InputStream in = null;
    try {
      in = mContext.getContentResolver().openInputStream(uri);
      //從輸入流中獲取到圖片
      Bitmap bm = BitmapFactory.decodeStream(in);
      in.close();
      return bm;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  /**
   * @param
   * @description 保存圖片到手機(jī)SD卡, 并返回圖片對應(yīng)的文件i
   * @author ldm
   * @time 2016/7/11 9:55
   */
  public static Uri saveBitmapToSdCard(Bitmap bm) {
    //自定義圖片名稱
    String name = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".png";
    //定義圖片存放的位置
    File tempFile = new File("/sdcard/Image/");
    if (!tempFile.exists()) {
      tempFile.mkdir();
    }
    String fileName = "/sdcard/Image/" + name;
    File pic = new File(fileName);
    try {
      FileOutputStream os = new FileOutputStream(pic);
      //對圖片進(jìn)行壓縮
      bm.compress(Bitmap.CompressFormat.PNG, 100, os);
      os.flush();
      os.close();
      return Uri.fromFile(pic);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

最后不要忘記在AndroidManifest.xml中添加 相應(yīng)權(quán)限:

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

感謝各位的閱讀!關(guān)于“Android編程中圖片操作類定義與用法的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI