Allow to set a watermark on pictures
This commit is contained in:
parent
2c385db985
commit
5820c607c2
|
@ -160,7 +160,7 @@ dependencies {
|
|||
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
|
||||
implementation 'com.github.smarteist:autoimageslider:1.3.2'
|
||||
//debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
|
||||
|
||||
implementation 'com.huangyz0918:androidwm-light:0.1.2'
|
||||
|
||||
//Flavors
|
||||
|
||||
|
|
|
@ -82,6 +82,8 @@ import com.bumptech.glide.request.transition.Transition;
|
|||
import com.smarteist.autoimageslider.IndicatorAnimations;
|
||||
import com.smarteist.autoimageslider.SliderAnimations;
|
||||
import com.smarteist.autoimageslider.SliderView;
|
||||
import com.watermark.androidwm_light.WatermarkBuilder;
|
||||
import com.watermark.androidwm_light.bean.WatermarkText;
|
||||
|
||||
import net.gotev.uploadservice.MultipartUploadRequest;
|
||||
import net.gotev.uploadservice.ServerResponse;
|
||||
|
@ -95,6 +97,8 @@ import org.jetbrains.annotations.NotNull;
|
|||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -428,13 +432,48 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
return textw;
|
||||
}
|
||||
|
||||
static private void upload(Activity activity, boolean pixelfedStory, Uri inUri, String fname, UploadServiceSingleBroadcastReceiver uploadReceiver) {
|
||||
static private void upload(Activity activity, boolean pixelfedStory, Account account, Uri inUri, String fname, UploadServiceSingleBroadcastReceiver uploadReceiver) {
|
||||
String uploadId = UUID.randomUUID().toString();
|
||||
if (uploadReceiver != null) {
|
||||
uploadReceiver.setUploadID(uploadId);
|
||||
}
|
||||
Uri uri;
|
||||
InputStream tempInput = null;
|
||||
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
boolean isWM = sharedpreferences.getBoolean(Helper.SET_WATERMARK, false);
|
||||
ContentResolver cr = activity.getContentResolver();
|
||||
String mime = cr.getType(inUri);
|
||||
if ((mime == null || mime.toLowerCase().contains("image")) && isWM) {
|
||||
String WMText = sharedpreferences.getString(Helper.SET_WATERMARK_TEXT, null);
|
||||
if( WMText == null || WMText.trim().length() == 0) {
|
||||
WMText = "@" + account.getUsername() + "@" + account.getInstance();
|
||||
}
|
||||
WatermarkText watermarkText = new WatermarkText(WMText)
|
||||
.setPositionX(0)
|
||||
.setPositionY(0.90)
|
||||
.setTextColor(Color.WHITE)
|
||||
.setTextShadow(0.1f, 3, 3, Color.LTGRAY)
|
||||
.setTextAlpha(200)
|
||||
.setRotation(0)
|
||||
.setTextSize(15);
|
||||
|
||||
try {
|
||||
Bitmap backgroundBitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver() , inUri);
|
||||
Bitmap bitmap = WatermarkBuilder
|
||||
.create(activity, backgroundBitmap)
|
||||
.loadWatermarkText(watermarkText)
|
||||
.getWatermark()
|
||||
.getOutputImage();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos);
|
||||
byte[] bitmapdata = bos.toByteArray();
|
||||
tempInput = new ByteArrayInputStream(bitmapdata);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Uri uri;
|
||||
FileOutputStream tempOut = null;
|
||||
String filename = inUri.toString().substring(inUri.toString().lastIndexOf("/"));
|
||||
int suffixPosition = filename.lastIndexOf(".");
|
||||
|
@ -442,7 +481,9 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
if (suffixPosition > 0) suffix = filename.substring(suffixPosition);
|
||||
try {
|
||||
File file;
|
||||
tempInput = activity.getContentResolver().openInputStream(inUri);
|
||||
if( tempInput == null) {
|
||||
tempInput = activity.getContentResolver().openInputStream(inUri);
|
||||
}
|
||||
if (fname.startsWith("fedilabins_")) {
|
||||
file = File.createTempFile("fedilabins_randomTemp1", suffix, activity.getCacheDir());
|
||||
} else {
|
||||
|
@ -462,9 +503,15 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
"fr.gouv.etalab.mastodon.fileProvider",
|
||||
file);
|
||||
} else {
|
||||
uri = FileProvider.getUriForFile(activity,
|
||||
"app.fedilab.android.fileProvider",
|
||||
file);
|
||||
if (BuildConfig.lite) {
|
||||
uri = FileProvider.getUriForFile(activity,
|
||||
"app.fedilab.lite.fileProvider",
|
||||
file);
|
||||
}else{
|
||||
uri = FileProvider.getUriForFile(activity,
|
||||
"app.fedilab.android.fileProvider",
|
||||
file);
|
||||
}
|
||||
}
|
||||
tempInput.close();
|
||||
tempOut.close();
|
||||
|
@ -479,7 +526,6 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
|
||||
try {
|
||||
final String fileName = FileNameCleaner.cleanFileName(fname);
|
||||
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String scheme = sharedpreferences.getString(Helper.SET_ONION_SCHEME + Helper.getLiveInstance(activity), "https");
|
||||
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
||||
int maxUploadRetryTimes = sharedpreferences.getInt(Helper.MAX_UPLOAD_IMG_RETRY_TIMES, 3);
|
||||
|
@ -942,9 +988,15 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
"fr.gouv.etalab.mastodon.fileProvider",
|
||||
photoFile);
|
||||
} else {
|
||||
photoFileUri = FileProvider.getUriForFile(this,
|
||||
"app.fedilab.android.fileProvider",
|
||||
photoFile);
|
||||
if (BuildConfig.lite) {
|
||||
photoFileUri = FileProvider.getUriForFile(this,
|
||||
"app.fedilab.lite.fileProvider",
|
||||
photoFile);
|
||||
}else{
|
||||
photoFileUri = FileProvider.getUriForFile(this,
|
||||
"app.fedilab.android.fileProvider",
|
||||
photoFile);
|
||||
}
|
||||
}
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
|
||||
startActivityForResult(takePictureIntent, TAKE_PHOTO);
|
||||
|
@ -1028,7 +1080,7 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
uploadReceiver = new UploadServiceSingleBroadcastReceiver(BasePixelfedComposeActivity.this);
|
||||
uploadReceiver.register(this);
|
||||
}
|
||||
new asyncPicture(activity, pixelfed_story.isChecked(), uri, filename, uploadReceiver).execute();
|
||||
new asyncPicture(activity, pixelfed_story.isChecked(), account, uri, filename, uploadReceiver).execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1303,7 +1355,7 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
String filename = FileNameCleaner.cleanFileName(url);
|
||||
upload_media.setEnabled(false);
|
||||
toot_it.setEnabled(false);
|
||||
upload(BasePixelfedComposeActivity.this, pixelfed_story.isChecked(), uri, filename, uploadReceiver);
|
||||
upload(BasePixelfedComposeActivity.this, pixelfed_story.isChecked(), account, uri, filename, uploadReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1926,13 +1978,15 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
UploadServiceSingleBroadcastReceiver uploadReceiver;
|
||||
String filename;
|
||||
boolean pixelfedStory;
|
||||
Account account;
|
||||
|
||||
asyncPicture(Activity activity, boolean pixelfedStory, Uri uri, String filename, UploadServiceSingleBroadcastReceiver uploadReceiver) {
|
||||
asyncPicture(Activity activity, boolean pixelfedStory, Account account, Uri uri, String filename, UploadServiceSingleBroadcastReceiver uploadReceiver) {
|
||||
this.activityWeakReference = new WeakReference<>(activity);
|
||||
this.uriFile = uri;
|
||||
this.uploadReceiver = uploadReceiver;
|
||||
this.filename = filename;
|
||||
this.pixelfedStory = pixelfedStory;
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1970,7 +2024,7 @@ public abstract class BasePixelfedComposeActivity extends BaseActivity implement
|
|||
filename = Helper.getFileName(this.activityWeakReference.get(), uriFile);
|
||||
}
|
||||
filesMap.put(filename, uriFile);
|
||||
upload(activityWeakReference.get(), pixelfedStory, uriFile, filename, uploadReceiver);
|
||||
upload(activityWeakReference.get(), pixelfedStory, account, uriFile, filename, uploadReceiver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,6 +95,9 @@ import com.bumptech.glide.request.transition.Transition;
|
|||
import com.github.irshulx.Editor;
|
||||
import com.github.irshulx.EditorListener;
|
||||
import com.github.irshulx.models.EditorTextStyle;
|
||||
import com.watermark.androidwm_light.WatermarkBuilder;
|
||||
import com.watermark.androidwm_light.bean.WatermarkImage;
|
||||
import com.watermark.androidwm_light.bean.WatermarkText;
|
||||
|
||||
import net.gotev.uploadservice.MultipartUploadRequest;
|
||||
import net.gotev.uploadservice.ServerResponse;
|
||||
|
@ -108,6 +111,8 @@ import org.jetbrains.annotations.NotNull;
|
|||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -504,8 +509,43 @@ public abstract class BaseTootActivity extends BaseActivity implements UploadSta
|
|||
if (uploadReceiver != null) {
|
||||
uploadReceiver.setUploadID(uploadId);
|
||||
}
|
||||
Uri uri;
|
||||
InputStream tempInput = null;
|
||||
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
boolean isWM = sharedpreferences.getBoolean(Helper.SET_WATERMARK, false);
|
||||
ContentResolver cr = activity.getContentResolver();
|
||||
String mime = cr.getType(inUri);
|
||||
if ((mime == null || mime.toLowerCase().contains("image")) && isWM) {
|
||||
String WMText = sharedpreferences.getString(Helper.SET_WATERMARK_TEXT, null);
|
||||
if( WMText == null || WMText.trim().length() == 0) {
|
||||
WMText = "@" + account.getUsername() + "@" + account.getInstance();
|
||||
}
|
||||
WatermarkText watermarkText = new WatermarkText(WMText)
|
||||
.setPositionX(0)
|
||||
.setPositionY(0.90)
|
||||
.setTextColor(Color.WHITE)
|
||||
.setTextShadow(0.1f, 3, 3, Color.LTGRAY)
|
||||
.setTextAlpha(200)
|
||||
.setRotation(0)
|
||||
.setTextSize(15);
|
||||
|
||||
try {
|
||||
Bitmap backgroundBitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver() , inUri);
|
||||
Bitmap bitmap = WatermarkBuilder
|
||||
.create(activity, backgroundBitmap)
|
||||
.loadWatermarkText(watermarkText)
|
||||
.getWatermark()
|
||||
.getOutputImage();
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos);
|
||||
byte[] bitmapdata = bos.toByteArray();
|
||||
tempInput = new ByteArrayInputStream(bitmapdata);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Uri uri;
|
||||
FileOutputStream tempOut = null;
|
||||
String filename = inUri.toString().substring(inUri.toString().lastIndexOf("/"));
|
||||
int suffixPosition = filename.lastIndexOf(".");
|
||||
|
@ -513,7 +553,9 @@ public abstract class BaseTootActivity extends BaseActivity implements UploadSta
|
|||
if (suffixPosition > 0) suffix = filename.substring(suffixPosition);
|
||||
try {
|
||||
File file;
|
||||
tempInput = activity.getContentResolver().openInputStream(inUri);
|
||||
if( tempInput == null) {
|
||||
tempInput = activity.getContentResolver().openInputStream(inUri);
|
||||
}
|
||||
if (fname.startsWith("fedilabins_")) {
|
||||
file = File.createTempFile("fedilabins_randomTemp1", suffix, activity.getCacheDir());
|
||||
} else {
|
||||
|
@ -533,9 +575,15 @@ public abstract class BaseTootActivity extends BaseActivity implements UploadSta
|
|||
"fr.gouv.etalab.mastodon.fileProvider",
|
||||
file);
|
||||
} else {
|
||||
uri = FileProvider.getUriForFile(activity,
|
||||
"app.fedilab.android.fileProvider",
|
||||
file);
|
||||
if (BuildConfig.lite) {
|
||||
uri = FileProvider.getUriForFile(activity,
|
||||
"app.fedilab.lite.fileProvider",
|
||||
file);
|
||||
}else{
|
||||
uri = FileProvider.getUriForFile(activity,
|
||||
"app.fedilab.android.fileProvider",
|
||||
file);
|
||||
}
|
||||
}
|
||||
tempInput.close();
|
||||
tempOut.close();
|
||||
|
@ -549,7 +597,7 @@ public abstract class BaseTootActivity extends BaseActivity implements UploadSta
|
|||
|
||||
try {
|
||||
final String fileName = FileNameCleaner.cleanFileName(fname);
|
||||
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
|
||||
String scheme = sharedpreferences.getString(Helper.SET_ONION_SCHEME + account.getInstance(), "https");
|
||||
String token = account.getToken();
|
||||
|
||||
|
@ -1483,9 +1531,15 @@ public abstract class BaseTootActivity extends BaseActivity implements UploadSta
|
|||
"fr.gouv.etalab.mastodon.fileProvider",
|
||||
photoFile);
|
||||
} else {
|
||||
photoFileUri = FileProvider.getUriForFile(this,
|
||||
"app.fedilab.android.fileProvider",
|
||||
photoFile);
|
||||
if (BuildConfig.lite) {
|
||||
photoFileUri = FileProvider.getUriForFile(this,
|
||||
"app.fedilab.lite.fileProvider",
|
||||
photoFile);
|
||||
}else{
|
||||
photoFileUri = FileProvider.getUriForFile(this,
|
||||
"app.fedilab.android.fileProvider",
|
||||
photoFile);
|
||||
}
|
||||
}
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
|
||||
startActivityForResult(takePictureIntent, TAKE_PHOTO);
|
||||
|
|
|
@ -473,6 +473,8 @@ public class BaseHelper {
|
|||
public static final String SET_ALLOW_STREAM = "set_allow_stream";
|
||||
public static final String SET_CUSTOM_USER_AGENT = "set_custom_user_agent";
|
||||
public static final String SET_VIDEO_CACHE = "set_video_cache";
|
||||
public static final String SET_WATERMARK = "set_watermark";
|
||||
public static final String SET_WATERMARK_TEXT = "set_watermark_text";
|
||||
//End points
|
||||
public static final String EP_AUTHORIZE = "/oauth/authorize";
|
||||
|
||||
|
|
Loading…
Reference in New Issue