Crop 选择相册图片并手动裁切

Crop 选择相册图片并手动裁切_第1张图片
一、 功能:
获取相册图片并手动进行裁切

二、简介
基于ASOP (Android Open-Source Project:Android 开放源代码项目)
维护更新频次不是很高
Github地址:https://github.com/jdamcd/android-crop

三、主要特点
Gradle构建和AAR
现代化的UI
向后兼容到SDK 10
配置简单

四、使用
依赖这个 AAR 发布在 Maven Central :
1
compile'com.soundcloud.android:android-crop:1.0.1@aar'
在你的 manifest 文件中申明 CropImageActivity :
1
android:name="com.soundcloud.android.crop.CropImageActivity" />
选择
这个库提供了一个实用的方法期待一个图像选择器:
1
Crop.pickImage(activity)
裁切
1
Crop.of(inputUri,outputUri).asSquare().start(activity)
监听裁切的结果:
1
2
3
4
5
6
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentresult){
    if(requestCode == Crop.REQUEST_CROP && resultCode ==RESULT_OK){
        doSomethingWithCroppedImage(outputUri);
    }
}
裁切见面的属性是可以定制的,详情请查看实例项目的主题
使用者
使用该库的应用程序包括:SoundCloudDepopPolyvore,TextSecure

五 Demo:
1、 gradle,在dependencies节点下添加:
compile'com.soundcloud.android.crop:lib_crop:1.0.0'
2、 在清单文件中注册裁剪功能所在的页面:
<activityandroid:name="com.soundcloud.android.crop.CropImageActivity"/>
3、 代码:
public class MainActivity extends AppCompatActivity{

private	ImageViewiv;

@Override
protected voidonCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
	iv=(ImageView)findViewById(R.id.iv);
}

//1、进入相册
public voidpick(View v){
   Crop.pickImage(this);
}

@Override
protected voidonActivityResult(intrequestCode,intresultCode,Intent data){
   super.onActivityResult(requestCode,resultCode,data);
   if(requestCode==Crop.REQUEST_PICK&&resultCode==RESULT_OK){
		//2、从相册回来,进入裁剪页
        beginCrop(data.getData());
    }else if(requestCode==Crop.REQUEST_CROP){
		//3、从裁剪页回来,设置图片
        handleCrop(resultCode,data);
    }
}

private voidhandleCrop(intresultCode,Intent result){
    if(resultCode==RESULT_OK){
        Uri uri=Crop.getOutput(result);
        inth=getResources().getDimensionPixelSize(R.dimen.height);
        Bitmap bitmap=UtilsBitmap.decodeUri(this,uri,h,h);
        iv.setImageBitmap(bitmap);
    }else if(resultCode==Crop.RESULT_ERROR){
        Toast.makeText(this,Crop.getError(result).getMessage(),Toast.LENGTH_SHORT).show();
    }
}

private voidbeginCrop(Uri source){
   Uri destination=Uri.fromFile(new File(getCacheDir(),"cropped_"+System.currentTimeMillis()+".jpg"));
   Crop.of(source,destination).asSquare().start(this);
}
}

Utils:
public classUtilsBitmap{
/**
 * 读取一个缩放后的图片,限定图片大小,避免OOM
 * @paramuri图片uri,支持“file://”、“content://”
 * @parammaxWidth最大允许宽度
 * @parammaxHeight最大允许高度
 * @return返回一个缩放后的Bitmap,失败则返回null
 */
public staticBitmapdecodeUri(Context context,Uri uri,intmaxWidth,intmaxHeight){
    BitmapFactory.Options options=newBitmapFactory.Options();
    options.inJustDecodeBounds=true; //只读取图片尺寸
    resolveUri(context,uri,options);

	//计算实际缩放比例
    intscale=1;
    for(inti=0;imaxWidth&&options.outWidth/scale>maxWidth*1.4)||
			(options.outHeight/scale>maxHeight&&options.outHeight/scale>maxHeight*1.4)){
				scale++;
		}else{
			break;
		}
    }

    options.inSampleSize=scale;
    options.inJustDecodeBounds=false;//读取图片内容
    options.inPreferredConfig=Bitmap.Config.RGB_565;//根据情况进行修改
    Bitmap bitmap=null;
    try{
        bitmap=resolveUriForBitmap(context,uri,options);
    }catch(Throwable e){
        e.printStackTrace();
    }
    returnbitmap;
}


private static voidresolveUri(Context context,Uri uri,BitmapFactory.Options options){
    if(uri==null){
        return;
    }
    String scheme=uri.getScheme();
    if(ContentResolver.SCHEME_CONTENT.equals(scheme)||
        ContentResolver.SCHEME_FILE.equals(scheme)){
			InputStream stream=null;
        try{
			stream=context.getContentResolver().openInputStream(uri);
			BitmapFactory.decodeStream(stream,null,options);
        }catch(Exception e){
			Log.w("resolveUri","Unable to open content: "+uri,e);
        }finally{
			if(stream!=null){
				try{
					stream.close();
				}catch(IOException e){
					Log.w("resolveUri","Unable to close content: "+uri,e);
				}
			}
        }
    }else if(ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)){
        Log.w("resolveUri","Unable to close content: "+uri);
    }else{
        Log.w("resolveUri","Unable to close content: "+uri);
    }
}

private staticBitmapresolveUriForBitmap(Context context,Uri uri,BitmapFactory.Options options){
    if(uri==null){
        return null;
    }

    Bitmap bitmap=null;
    String scheme=uri.getScheme();
    if(ContentResolver.SCHEME_CONTENT.equals(scheme)||
        ContentResolver.SCHEME_FILE.equals(scheme)){
        InputStream stream=null;
        try{
			stream=context.getContentResolver().openInputStream(uri);
			bitmap=BitmapFactory.decodeStream(stream,null,options);
        }catch(Exception e){
			Log.w("resolveUriForBitmap","Unable to open content: "+uri,e);
        }finally{
			if(stream!=null){
				try{
					stream.close();
				}catch(IOException e){
					Log.w("resolveUriForBitmap","Unable to close content: "+uri,e);
				}
			}
		}
	}else if(ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)){
        Log.w("resolveUriForBitmap","Unable to close content: "+uri);
    }else{
        Log.w("resolveUriForBitmap","Unable to close content: "+uri);
    }

    returnbitmap;
}
}





你可能感兴趣的:(Android应用)