您的位置:首页 > 移动开发 > Android开发

android 裁剪图片大小 控制图片尺寸 .

2012-11-03 20:50 471 查看

用BitmapFactory获取适合屏幕大小的图片 和自带的图片裁剪工具

[java]
view plaincopyprint?

[java] view plaincopyprint?


[java]
view plaincopyprint?

[java] view plaincopyprint?package com.lin.image;

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

public class ImageScaleActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private Button selectImageBtn;
private Button cutImageBtn;
private ImageView imageView;
private static final int IMAGE_SELECT=1;
private static final int IMAGE_CUT=2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

selectImageBtn=(Button)findViewById(R.id.selectImageBtn);
cutImageBtn=(Button)findViewById(R.id.catImageBtn);
imageView=(ImageView)findViewById(R.id.imageView);

cutImageBtn.setOnClickListener(this);
selectImageBtn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
package com.lin.image;

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

public class ImageScaleActivity extends Activity  implements View.OnClickListener{
/** Called when the activity is first created. */
private Button selectImageBtn;
private Button cutImageBtn;
private ImageView imageView;
private static final int  IMAGE_SELECT=1;
private static final int  IMAGE_CUT=2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

selectImageBtn=(Button)findViewById(R.id.selectImageBtn);
cutImageBtn=(Button)findViewById(R.id.catImageBtn);
imageView=(ImageView)findViewById(R.id.imageView);

cutImageBtn.setOnClickListener(this);
selectImageBtn.setOnClickListener(this);

}

@Override
public void onClick(View v) {


[java]
view plaincopyprint?

<SPAN style="WHITE-SPACE: pre"> </SPAN>//截取适合屏幕大小的图片

if(v==selectImageBtn){
Intent intent=new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_SELECT);
}//一般用于头像等需要设置指定大小

else if(v==cutImageBtn){
Intent intent=getImageClipIntent();
startActivityForResult(intent, IMAGE_CUT);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// TODO Auto-generated method stub

if(resultCode==RESULT_OK){
if(requestCode==IMAGE_SELECT){
Uri imageFileUri =intent.getData();
int dw=getWindowManager().getDefaultDisplay().getWidth();
int dh=getWindowManager().getDefaultDisplay().getHeight()/2;
//已屏幕宽 和一般的高作为图片显示的最大尺寸

try{
BitmapFactory.Options factory=new BitmapFactory.Options();
factory.inJustDecodeBounds=true; //当为true时 允许查询图片不为 图片像素分配内存

Bitmap bmp=BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri),null,factory);
int hRatio=(int)Math.ceil(factory.outHeight/(float)dh); //图片是高度的几倍

int wRatio=(int)Math.ceil(factory.outWidth/(float)dw); //图片是宽度的几倍

System.out.println("hRatio:"+hRatio+" wRatio:"+wRatio);
//缩小到 1/ratio的尺寸和 1/ratio^2的像素

if(hRatio>1||wRatio>1){
if(hRatio>wRatio){
factory.inSampleSize=hRatio;
}
else
factory.inSampleSize=wRatio;
}
factory.inJustDecodeBounds=false;
bmp=BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri),null,factory);
imageView.setImageBitmap(bmp);
}catch(Exception ex){

}
}
else if(requestCode==IMAGE_CUT){
Bitmap bmp=intent.getParcelableExtra("data");
imageView.setImageBitmap(bmp);
}
}

}
/**
* 获取剪切后的图片
*/
public static Intent getImageClipIntent() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);//裁剪框比例

intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);//输出图片大小

intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}

}

//截取适合屏幕大小的图片
if(v==selectImageBtn){
Intent intent=new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_SELECT);
}//一般用于头像等需要设置指定大小
else if(v==cutImageBtn){
Intent intent=getImageClipIntent();
startActivityForResult(intent, IMAGE_CUT);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// TODO Auto-generated method stub
if(resultCode==RESULT_OK){
if(requestCode==IMAGE_SELECT){
Uri imageFileUri =intent.getData();
int dw=getWindowManager().getDefaultDisplay().getWidth();
int dh=getWindowManager().getDefaultDisplay().getHeight()/2;
//已屏幕宽 和一般的高作为图片显示的最大尺寸
try{
BitmapFactory.Options factory=new BitmapFactory.Options();
factory.inJustDecodeBounds=true; //当为true时  允许查询图片不为 图片像素分配内存
Bitmap bmp=BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri),null,factory);
int hRatio=(int)Math.ceil(factory.outHeight/(float)dh); //图片是高度的几倍
int wRatio=(int)Math.ceil(factory.outWidth/(float)dw); //图片是宽度的几倍
System.out.println("hRatio:"+hRatio+"  wRatio:"+wRatio);
//缩小到  1/ratio的尺寸和 1/ratio^2的像素
if(hRatio>1||wRatio>1){
if(hRatio>wRatio){
factory.inSampleSize=hRatio;
}
else
factory.inSampleSize=wRatio;
}
factory.inJustDecodeBounds=false;
bmp=BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri),null,factory);
imageView.setImageBitmap(bmp);
}catch(Exception ex){

}
}
else if(requestCode==IMAGE_CUT){
Bitmap bmp=intent.getParcelableExtra("data");
imageView.setImageBitmap(bmp);
}
}

}
/**
* 获取剪切后的图片
*/
public static Intent getImageClipIntent() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);//裁剪框比例
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);//输出图片大小
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
return intent;
}

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: