Android拍照得到全尺寸图片并进行压缩
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<LinearLayoutxmlns: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="TakePhoto"/>
<Button
android:id="@+id/get_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="getPhoto"/>
<ImageView
android:id="@+id/picture"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
packagecom.example.choosepictest;
importjava.io.File;
importjava.io.IOException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.provider.MediaStore;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
publicclassMainActivityextendsActivityimplementsOnClickListener{
staticfinalintREQUEST_IMAGE_CAPTURE=1;
privateButtontakePhoto;
privateButtongetPhoto;
privateImageViewpicture;
privateUriimgUri;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
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
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.take_photo:
dispatchTakePictureIntent();
break;
default:
break;
}
}
//保存全尺寸照片
StringmCurrentPhotoPath;
privatevoiddispatchTakePictureIntent(){
FileappDir=newFile(Environment.getExternalStorageDirectory(),
"/etoury/picCache");
if(!appDir.exists()){
appDir.mkdir();
}
StringtimeStamp=newSimpleDateFormat("yyyyMMdd_HHmmss")
.format(newDate());
StringfileName=timeStamp+".jpg";
FileoutputImage=newFile(appDir,fileName);
try{
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch(IOExceptione){
e.printStackTrace();
}
mCurrentPhotoPath=outputImage.getAbsolutePath();
imgUri=Uri.fromFile(outputImage);
//意图相机
Intentintent=newIntent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
//如果有相机
if(intent.resolveActivity(getPackageManager())!=null){
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
}
}
//解码缩放图片(DecodeaScaledImage)
privatevoidsetPic(){
//GetthedimensionsoftheView
inttargetW=picture.getWidth();
inttargetH=picture.getHeight();
//Getthedimensionsofthebitmap
BitmapFactory.OptionsbmOptions=newBitmapFactory.Options();
//该值设为true那么将不返回实际的bitmap,也不给其分配内存空间这样就避免内存溢出了。但是允许我们查询图片的信息这其中就包括图片大小信息
bmOptions.inJustDecodeBounds=true;
BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
intphotoW=bmOptions.outWidth;
intphotoH=bmOptions.outHeight;
//Determinehowmuchtoscaledowntheimage
//Math.min求最小值
intscaleFactor=Math.min(photoW/targetW,photoH/targetH);
//DecodetheimagefileintoaBitmapsizedtofilltheView
bmOptions.inJustDecodeBounds=false;
//设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误
bmOptions.inSampleSize=scaleFactor;
//如果inPurgeable设为True的话表示使用BitmapFactory创建的Bitmap,用于存储Pixel的内存空间在系统内存不足时可以被回收
bmOptions.inPurgeable=true;
Bitmapbitmap=BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
picture.setImageBitmap(bitmap);
}
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
if(resultCode==RESULT_OK){
switch(requestCode){
caseREQUEST_IMAGE_CAPTURE:
setPic();
break;
default:
break;
}
}
}
}
以上代码就是本文给大家介绍的Android拍照得到全尺寸图片并进行压缩的全部内容,希望大家喜欢。