Skip to content

Commit

Permalink
refact
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Sep 27, 2021
1 parent b42f93c commit a47e9cd
Show file tree
Hide file tree
Showing 30 changed files with 701 additions and 1,831 deletions.
151 changes: 0 additions & 151 deletions README-zh.md

This file was deleted.

161 changes: 28 additions & 133 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,155 +1,50 @@
[中文说明](README-zh.md)

# ImageFileSelector

[![Build Status](https://travis-ci.org/sw926/ImageFileSelector.svg?branch=master)](https://travis-ci.org/sw926/ImageFileSelector)

##### Use the system software to select, compress, crop images

##### support Android Api Level >= 16
轻量级的选取图片和裁切图片的库,使用系统自带的软件实现。

Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap].
支持Android版本 Api Level >= 16

How to use
----------
Maven

```xml
<dependency>
<groupId>com.sw926.imagefileselector</groupId>
<artifactId>library</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
```
Gradle
## 使用方法

```gradle
compile 'com.sw926.imagefileselector:library:1.1.0-SNAPSHOT'
compile 'com.sw926.imagefileselector:library:2.0.0'
```


Select Image
----------
Init
在 Activity 或者 Fragment Start 之前进行初始化:

``` java
ImageFileSelector mImageFileSelector = new ImageFileSelector(this);
mImageFileSelector.setCallback(new ImageFileSelector.Callback() {
mImageFileSelector = new ImageFileSelector(this);
mImageFileSelector.setOutPutImageSize(w, h);
mImageFileSelector.setQuality(80);
mImageFileSelector.setListener(new ImageFileResultListener() {
@Override
public void onError(@NotNull ErrorResult errorResult) {
switch (errorResult) {
case permissionDenied:
break;
case canceled:
break;
case error:
break;
}
public void onSuccess(@NonNull String filePath) {
loadImage(filePath);
mCurrentSelectFile = new File(filePath);
mBtnCrop.setVisibility(View.VISIBLE);
}

@Override
public void onSuccess(@NotNull String file) {
public void onCancel() {
Toast.makeText(ExampleActivity.this, "Canceled", Toast.LENGTH_LONG).show();
}
});
```
Add code to you Activity or Fragment
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mImageFileSelector.onActivityResult(this, requestCode, resultCode, data);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mImageFileSelector.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mImageFileSelector.onRestoreInstanceState(savedInstanceState);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mImageFileSelector.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
```
Setting parameters
```java
// Set the output file size
mImageFileSelector.setOutPutImageSize(800, 600);
Set the picture save quality, 0 to 100
mImageFileSelector.setQuality(80);
```
Start select image
```java
// take picture from camera
mImageFileSelector.takePhoto(this, requestCode);
// select image from sdcard
mImageFileSelector.selectImage(this, requestCode);
// Set the save path of the image,default: /sdcard/Android/data/{packagename}/cache/images/
mImageFileSelector.setOutPutPath();
@Override
public void onError() {
Toast.makeText(ExampleActivity.this, "Unknown Error", Toast.LENGTH_LONG).show();
}
});
```

在选择图片的地方调用:

Crop Image
----------
Init
```java
ImageCropper mImageCropper = new ImageCropper();
mImageCropper.setCallback(new ImageCropper.ImageCropperCallback() {
@Override
public void onError(@NotNull ImageCropper.CropperErrorResult result) {
switch (result) {
case error:
break;
case canceled:
break;
case notSupport:
break;
}
}

@Override
public void onSuccess(@NotNull String outputFile) {
}
});
``` java
mImageFileSelector.takePhoto();
// 或者
mImageFileSelector.selectImage();
```
Add code to you Activity or Fragment
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mImageCropper.onActivityResult(requestCode, resultCode, data);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mImageCropper.onSaveInstanceState(outState);
}
## 说明

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mImageCropper.onRestoreInstanceState(savedInstanceState);
}
```
Setting parameters
```java
// Sets the picture aspect ratio
mImageCropper.setOutPutAspect(1, 1);
// Sets the image size
mImageCropper.setOutPut(800, 800);
// Sets whether to scale
mImageCropper.setScale(true);
```
crop image
```java
mImageCropper.cropImage(file);
```
[snap]: https://oss.sonatype.org/content/repositories/snapshots/com/sw926/imagefileselector/library/
在 appcompat 支持 ActivityResultLauncher 后,选择图片已经非常简单,这个项目只是做了简单的封装,添加了拍照和图片压缩。选择图片不需要任何权限,如果 App 的 `AndroidManifest.xml` 没有添加 `<uses-permission android:name="android.permission.CAMERA" />`
,拍照也不需要申请权限。
9 changes: 4 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
compileSdkVersion 31

defaultConfig {
applicationId "com.sw926.imagefileselector.example"
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 31
versionCode 10
versionName "1.0.7"
}
Expand All @@ -26,9 +25,9 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.13.1'
testImplementation 'junit:junit:4.13.2'
implementation project(':library')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sw926.imagefileselector.example">

<uses-permission android:name="android.permission.CAMERA"/>

<application
android:allowBackup="false"
Expand All @@ -14,7 +13,9 @@
android:configChanges="keyboardHidden"
android:label="From Activity"
android:windowSoftInputMode="stateHidden" />
<activity android:name=".LaunchActivity">
<activity
android:name=".LaunchActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Loading

0 comments on commit a47e9cd

Please sign in to comment.