
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
io.github.singlewolf:TakePhoto
Advanced tools
基于TakePhoto框架上修改,以提供给老旧项目使用
TakePhoto
是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库
方式一:通过继承的方式
TakePhotoActivity
、TakePhotoFragmentActivity
、TakePhotoFragment
三者之一。getTakePhoto()
获取TakePhoto
实例进行相关操作。void takeSuccess(String imagePath);
void takeFail(String msg);
void takeCancel();
此方式使用简单,满足的大部分的使用需求,具体使用详见simple。如果通过继承的方式无法满足实际项目的使用,可以通过下面介绍的方式。
方式二:通过组装的方式
TakePhoto takePhoto=new TakePhotoImpl(getActivity(),this);
onCreate
,onActivityResult
,onSaveInstanceState
方法中调用TakePhoto对用的方法。TakeResultListener
相关方法中获取结果。TakePhoto提供拍照,从相册选择,从文件中选择三种方式获取图片。
/**
* 从文件中获取图片(不裁剪)
*/
void onPickFromDocuments();
/**
* 从相册中获取图片(不裁剪)
*/
void onPickFromGallery();
/**
* 从相机获取图片(不裁剪)
* @param outPutUri 图片保存的路径
*/
void onPickFromCapture(Uri outPutUri);
以上三种方式均提供对应的裁剪API。
注:
由于不同Android Rom厂商对系统有不同程度的定制,有可能导致某种选择图片的方式不支持,所以为了提高TakePhoto
的兼容性,当某种选的图片的方式不支持时,TakePhoto
会自动切换成使用另一种选择图片的方式进行图片选择。
TakePhoto
支持对图片进行裁剪,无论是拍照的照片,还是从相册、文件中选择的图片。你只需要调用TakePhoto
的相应方法即可:
/**
* 从相机获取图片并裁剪
* @param outPutUri 图片裁剪之后保存的路径
* @param options 裁剪配置
*/
void onPickFromCaptureWithCrop(Uri outPutUri, CropOptions options);
/**
* 从相册中获取图片并裁剪
* @param outPutUri 图片裁剪之后保存的路径
* @param options 裁剪配置
*/
void onPickFromGalleryWithCrop(Uri outPutUri, CropOptions options);
/**
* 从文件中获取图片并裁剪
* @param outPutUri 图片裁剪之后保存的路径
* @param options 裁剪配置
*/
void onPickFromDocumentsWithCrop(Uri outPutUri, CropOptions options);
另外,TakePhoto也支持你对指定图片进行裁剪:
/**
* 裁剪图片
* @param imageUri 要裁剪的图片
* @param outPutUri 图片裁剪之后保存的路径
* @param options 裁剪配置
*/
void onCrop(Uri imageUri, Uri outPutUri, CropOptions options)throws TException;
CropOptions
是用于裁剪的配置类,通过它你可以对图片的裁剪比例,最大输出大小,以及是否使用TakePhoto
自带的裁剪工具进行裁剪等,进行个性化配置。
Usage:
CropOptions cropOptions=new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create();
getTakePhoto().onPickFromDocumentsWithCrop(imageUri,cropOptions);
//或
getTakePhoto().onCrop(imageUri,outPutUri,cropOptions);
注:
由于不同Android Rom厂商对系统有不同程度的定制,有可能系统中没有自带或第三方的裁剪工具,所以为了提高TakePhoto
的兼容性,当系统中没有自带或第三方裁剪工具时,TakePhoto
会自动切换到使用TakePhoto
自带的裁剪工具进行裁剪。
你可以选择是否对图片进行压缩处理,你只需要告诉它你是否要启用压缩功能以及CompressConfig
即可。
/**
* 启用图片压缩
* @param config 压缩图片配置
* @param showCompressDialog 压缩时是否显示进度对话框
* @return
*/
TakePhoto onEnableCompress(CompressConfig config,boolean showCompressDialog);
Usage:
getTakePhoto().onEnableCompress(compressConfig,true).onPickFromGalleryWithCrop(imageUri,cropOptions);
如果你启用了图片压缩,TakePhoto
会使用CompressImage
对图片进行压缩处理,CompressImage
目前支持对图片的尺寸以及图片的质量进行压缩。默认情况下,CompressImage
开启了尺寸与质量双重压缩。
另外,你也可以对指定图片进行压缩:
Usage:
new CompressImageImpl(compressConfig).compress(picturePath, new CompressImage.CompressListener() {
@Override
public void onCompressSuccess(String imgPath) {//图片压缩成功
}
@Override
public void onCompressFailed(String imagePath,String msg) {//图片压缩失败
}
});
CompressConfig
是用于图片压缩的配置类,你可以通过CompressConfig.Builder
对图片压缩后的尺寸以及质量进行相关设置。如果你想改变压缩的方式可以通过CompressConfig.Builder
进行相关设置。
Usage:
CompressConfig compressConfig=new CompressConfig.Builder().setMaxSize(50*1024).setMaxPixel(800).create();
getTakePhoto().onEnableCompress(compressConfig,true).onPickFromGallery();
TakePhot
o是基于Android官方标准API编写的,适配了目前市场上主流的Rom。如果你在使用过程中发现了适配问题,可以提交Issues。
Activity
,TakePhoto
在onSaveInstanceState
与 onCreate
做了相应的恢复处理。TakePhoto
的Activity
添加android:configChanges="orientation|keyboardHidden|screenSize"配置。<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Gradle:
implementation 'io.github.singlewolf:TakePhoto:1.0.0'
Maven:
<dependency>
<groupId>io.github.singlewolf</groupId>
<artifactId>TakePhoto</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
特殊原因,经过改造后适用于比较老旧的项目,新项目谨慎使用。
FAQs
TakePhoto
We found that io.github.singlewolf:TakePhoto demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.