Fix mark / reset

This commit is contained in:
max-klaus 2021-02-03 21:30:43 +03:00
parent 894a36fed8
commit 4dc202dbc9

View file

@ -28,7 +28,6 @@ import org.openplacereviews.opendb.util.exception.FailedVerificationException;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
@ -102,10 +101,11 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
private boolean handleSelectedImage(final Uri uri) { private boolean handleSelectedImage(final Uri uri) {
boolean success = false; boolean success = false;
InputStream inputStream = null; InputStream inputStream = null;
int[] imageDimensions = null;
try { try {
inputStream = app.getContentResolver().openInputStream(uri); inputStream = app.getContentResolver().openInputStream(uri);
if (inputStream != null) { if (inputStream != null) {
success = uploadImageToPlace(inputStream); imageDimensions = calcImageDimensions(inputStream);
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error(e); LOG.error(e);
@ -113,12 +113,27 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
} finally { } finally {
Algorithms.closeStream(inputStream); Algorithms.closeStream(inputStream);
} }
if (imageDimensions != null && imageDimensions.length == 2) {
try {
inputStream = app.getContentResolver().openInputStream(uri);
if (inputStream != null) {
int width = imageDimensions[0];
int height = imageDimensions[1];
success = uploadImageToPlace(inputStream, width, height);
}
} catch (Exception e) {
LOG.error(e);
app.showToastMessage(R.string.cannot_upload_image);
} finally {
Algorithms.closeStream(inputStream);
}
}
return success; return success;
} }
private boolean uploadImageToPlace(InputStream image) throws IOException { private boolean uploadImageToPlace(InputStream image, int width, int height) {
boolean success = false; boolean success = false;
InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image)); InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image, width, height));
String baseUrl = OPRConstants.getBaseUrl(app); String baseUrl = OPRConstants.getBaseUrl(app);
// all these should be constant // all these should be constant
String url = baseUrl + "api/ipfs/image"; String url = baseUrl + "api/ipfs/image";
@ -178,16 +193,18 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
} }
} }
private byte[] compressImageToJpeg(InputStream image) throws IOException { private int[] calcImageDimensions(InputStream image) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(image); BufferedInputStream bufferedInputStream = new BufferedInputStream(image);
bufferedInputStream.mark(1024 * 200);
BitmapFactory.Options opts = new BitmapFactory.Options(); BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true; opts.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bufferedInputStream, null, opts); BitmapFactory.decodeStream(bufferedInputStream, null, opts);
int w = opts.outWidth; return new int[] { opts.outWidth, opts.outHeight };
int h = opts.outHeight; }
bufferedInputStream.reset();
ByteArrayOutputStream os = new ByteArrayOutputStream(); private byte[] compressImageToJpeg(InputStream image, int width, int height) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(image);
int w = width;
int h = height;
boolean scale = false; boolean scale = false;
int divider = 1; int divider = 1;
while (w > MAX_IMAGE_LENGTH || h > MAX_IMAGE_LENGTH) { while (w > MAX_IMAGE_LENGTH || h > MAX_IMAGE_LENGTH) {
@ -198,12 +215,13 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
} }
Bitmap bmp; Bitmap bmp;
if (scale) { if (scale) {
opts = new BitmapFactory.Options(); BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = divider; opts.inSampleSize = divider;
bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts); bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts);
} else { } else {
bmp = BitmapFactory.decodeStream(bufferedInputStream); bmp = BitmapFactory.decodeStream(bufferedInputStream);
} }
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, os); bmp.compress(Bitmap.CompressFormat.JPEG, 90, os);
return os.toByteArray(); return os.toByteArray();
} }