溫馨提示×

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

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

Android實(shí)現(xiàn)拍照及圖片顯示效果

發(fā)布時(shí)間:2020-10-18 06:16:50 來(lái)源:腳本之家 閱讀:255 作者:ZhangJianIsAStark 欄目:移動(dòng)開發(fā)

本文實(shí)例為大家分享了Android拍照及圖片顯示的具體代碼,供大家參考,具體內(nèi)容如下

1、功能聲明

當(dāng)應(yīng)用需要使用相機(jī)、NFC等外設(shè)時(shí),需要在AndroidManifest.xml中進(jìn)行聲明。
這樣,當(dāng)設(shè)備缺少這些外設(shè)時(shí),應(yīng)用商店的安裝程序可以拒絕安裝設(shè)備。

聲明示例代碼如下:

<uses-feature android:name="android.hardware.camera2"
    <!-- required為false時(shí),不強(qiáng)制要求設(shè)備支持該功能 -->
    <!-- 如果不設(shè)置該值,一旦設(shè)備不支持camera,就不能安裝該應(yīng)用-->
    android:required="false"/>

2、創(chuàng)建指向文件的File對(duì)象

拍攝的照片可以存放到設(shè)備的外部存儲(chǔ)區(qū)。

Android為不同的應(yīng)用分配的獨(dú)有的存儲(chǔ)區(qū)域,同時(shí)按照存儲(chǔ)數(shù)據(jù)的類型對(duì)存儲(chǔ)區(qū)域做了進(jìn)一步地劃分。
設(shè)置照片存儲(chǔ)區(qū)域的代碼示例如下所示:

public File getPhotoFile(Crime crime) {
  //獲取應(yīng)用對(duì)應(yīng)的存儲(chǔ)照片的外部存儲(chǔ)路徑
  File externalFilesDir = mContext
      .getExternalFilesDir(Environment.DIRECTORY_PICTURES);

  if (externalFilesDir == null) {
    return null;
  }

  //創(chuàng)建指向文件的File對(duì)象
  return new File(externalFilesDir, crime.getPhotoFilename());
}
.............
//每個(gè)crime對(duì)應(yīng)的文件名
public String getPhotoFilename() {
  return "IMG_" + getId().toString() + ".jpg";
}

3、觸發(fā)拍照

可以使用MediaStore.ACTION_CAPTURE_IMAGE類型的Intent觸發(fā)拍照,示例代碼如下:

mPhotoButton = (ImageButton) v.findViewById(R.id.crime_camera);

//隱式Intent觸發(fā)相機(jī)拍照
final Intent captureImageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//mPhotoFile保存著上文創(chuàng)建的指向指定地址的File
//此處判斷是否有能夠處理隱式Intent的組件
boolean canTakePhoto = mPhotoFile != null
    && captureImageIntent.resolveActivity(packageManager) != null;
mPhotoButton.setEnabled(canTakePhoto);

if (canTakePhoto) {
  //得到File文件對(duì)應(yīng)的Uri地址
  Uri uri = Uri.fromFile(mPhotoFile);

  //將Uri地址存入到Intent中,相機(jī)拍照得到的圖像將會(huì)存入到該Uri地址對(duì)應(yīng)的File里
  captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}

mPhotoButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    startActivityForResult(captureImageIntent, REQUEST_PHOTO);
  }
});

4、處理拍照結(jié)果

拍照完成后,將可以加載得到圖片了。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  .........
  } else if (requestCode == REQUEST_PHOTO) {
    updatePhotoView();
  }
  .........
}

private void updatePhotoView() {
  if (mPhotoFile == null || !mPhotoFile.exists()) {
    mPhotoView.setImageDrawable(null);
  } else {
    //加載圖片對(duì)應(yīng)的縮略圖
    Bitmap bitmap = PictureUtils.getScaledBitmap(mPhotoFile.getPath(), getActivity());
    mPhotoView.setImageBitmap(bitmap);
  }
}

Bitmap只存儲(chǔ)實(shí)際像素?cái)?shù)據(jù),因此即使原始照片已經(jīng)壓縮過(guò),但存入Bitmap對(duì)象時(shí),文件并不會(huì)被壓縮。

因此加載圖片時(shí),需要先按照給定區(qū)域的大小合理的縮放文件。 然后,用Bitmap加載縮放后的文件,示例代碼如下:

//在具體視圖未加載前,無(wú)法得到視圖的實(shí)際大小
//因此根據(jù)屏幕尺寸,使用估算值進(jìn)行縮放
public static Bitmap getScaledBitmap(String path, Activity activity) {
  Point size = new Point();
  activity.getWindowManager().getDefaultDisplay().getSize(size);

  return getScaledBitmap(path, size.x, size.y);
}

public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;

  //按照正常尺寸解析文件
  BitmapFactory.decodeFile(path, options);

  //得到原始文件的寬和高
  float srcWidth = options.outWidth;
  float srcHeight = options.outHeight;

  //inSampleSize表示水平/豎直抽樣比
  //例如,inSampleSize為2時(shí),水平和數(shù)值均在原始基礎(chǔ)上,每2個(gè)點(diǎn)抽取1個(gè)點(diǎn)
  //于是,新圖的大小變?yōu)樵瓉?lái)的1/4
  int inSampleSize = 1;
  if (srcHeight > destHeight || srcWidth > destWidth) {
    if (srcWidth > srcHeight) {
      inSampleSize = Math.round(srcHeight / destHeight);
    } else {
      inSampleSize = Math.round(srcWidth / destWidth);
    }
   }

  options = new BitmapFactory.Options();
  options.inSampleSize = inSampleSize;

  //按新的抽樣比,重新解析文件
  return BitmapFactory.decodeFile(path, options);
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

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

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

AI