我想从相机/画廊中获取图像,将其压缩并发送到Firebase。
因此,我完成了此操作(selectImage
是从相机/图库加载的功能):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) { //dalla fotocamera
if(data != null) { //caso galleria
mMediaUri = data.getData();
}
CompressBitmap task = new CompressBitmap(this);
String newPath=null;
try {
newPath = task.execute(String.valueOf(mMediaUri)).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Picasso.with(this).load(newPath).into(addPhoto, new Callback() {
public void onSuccess() {
// stopProgressDialog(circleDialog);
showErrorMessage(RegisterActivity.this,getString(R.string.okAddPhoto));
}
@Override
public void onError() {
showErrorMessage(RegisterActivity.this,getString(R.string.errAddPhoto));
}
});
}
} else {
}
}
private void selectImage() {
final CharSequence[] items = {getString(R.string.camera), getString(R.string.gallery), getString(R.string.indietro)};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.addPhoto);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals(getString(R.string.camera))) {
mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(photoIntent, REQUEST_TAKE_PHOTO);
dialog.dismiss();
} else if (items[item].equals(getString(R.string.gallery))) {
Intent pickPhoto = new Intent(Intent.ACTION_GET_CONTENT);
pickPhoto.setType("image/*");
startActivityForResult(pickPhoto,REQUEST_PICK_PHOTO);
dialog.dismiss();
} else if (items[item].equals(getString(R.string.indietro))) {
dialog.dismiss();
}
}
});
builder.show();
}
private Uri getOutputMediaFileUri(int mediaTypeImage) {
//check for external storage
if(isExternalStorageAvaiable()) {
File mediaStorageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
String fileName = "";
String fileType = "";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
fileName = "IMG_"+timeStamp;
fileType = ".jpg";
File mediaFile;
try {
mediaFile = File.createTempFile(fileName,fileType,mediaStorageDir);
Log.i("st","File: "+Uri.fromFile(mediaFile));
} catch (IOException e) {
e.printStackTrace();
Log.i("St","Error creating file: " + mediaStorageDir.getAbsolutePath() +fileName +fileType);
return null;
}
return Uri.fromFile(mediaFile);
}
//something went wrong
return null;
}
private boolean isExternalStorageAvaiable() {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else {
return false;
}
}
加载大图像时,我只会遇到问题。因此,我认为在将位图图像加载到
ImageView
之前应先对其进行压缩。所以我找到了这段代码,并使用了它:private class CompressBitmap extends AsyncTask<String, Void, String> {
private Context context;
private static final float maxHeight = 1280.0f;
private static final float maxWidth = 1280.0f;
public CompressBitmap(Context context) {
this.context=context;
}
protected String doInBackground(String... strings) {
if(strings.length == 0 || strings[0] == null) {
return null;
} return compressImage(strings[0]);
}
public String compressImage(String imagePath) {
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
float imgRatio = (float) actualWidth / (float) actualHeight;
float maxRatio = maxWidth / maxHeight;
Log.d("actual height, width:", String.valueOf(actualHeight) + "," + String.valueOf(actualWidth));
Log.d("imgRatio,maxRatio", String.valueOf(imgRatio)+","+String.valueOf(maxRatio));
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];
try {
bmp = BitmapFactory.decodeFile(imagePath, options);
Log.d("bmp",String.valueOf(bmp));
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
try {
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
Log.d("scaledBitmap",String.valueOf(scaledBitmap));
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
float ratioX = actualWidth / (float) options.outWidth;
float ratioY = actualHeight / (float) options.outHeight;
float middleX = actualWidth / 2.0f;
float middleY = actualHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
if (bmp != null) {
bmp.recycle();
}
ExifInterface exif;
try {
exif = new ExifInterface(imagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream out = null;
String filepath = imagePath;//getFilename();
try {
//new File(imageFilePath).delete();
out = new FileOutputStream(filepath);
//write the compressed bitmap at the destination specified by filename.
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return filepath;
}
public String getFilename() {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/ImageCompApp/Images");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
return uriString;
}
}
public static String getFilename() {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/ImageCompApp/Images");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
return uriString;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}
但我收到此错误消息:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A6174: open failed: ENOENT (No such file or directory)
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A6174: open failed: ENOENT (No such file or directory)
W/System.err: java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: width and height must be > 0
W/System.err: at java.util.concurrent.FutureTask.report(FutureTask.java:94)
W/System.err: at java.util.concurrent.FutureTask.get(FutureTask.java:164)
W/System.err: at android.os.AsyncTask.get(AsyncTask.java:498)
W/System.err: at com.sfproduction.happypark.RegisterActivity.onActivityResult(RegisterActivity.java:566)
W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6456)
W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:3729)
W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
W/System.err: at android.app.ActivityThread.-wrap16(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5461)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: java.lang.IllegalArgumentException: width and height must be > 0
W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:830)
W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:776)
W/System.err: at com.sfproduction.happypark.RegisterActivity$CompressBitmap.compressImage(RegisterActivity.java:810)
W/System.err: at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:769)
W/System.err: at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:753)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.sfproduction.happypark, PID: 2494
Theme: themes:{}
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:830)
at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
at android.graphics.Bitmap.createBitmap(Bitmap.java:776)
at com.sfproduction.happypark.RegisterActivity$CompressBitmap.compressImage(RegisterActivity.java:810)
at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:769)
at com.sfproduction.happypark.RegisterActivity$CompressBitmap.doInBackground(RegisterActivity.java:753)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
如果使用
InputStream
,则会得到bmp == null
。我将所有必需的权限都放在了
AndroidManifest
中。如何解决此错误,或者使用其他方法进行压缩?
最佳答案
您正在使用毕加索!您没有理由自己进行任何“压缩” 1!
使用Picasso提供的方法为您进行缩放。使用resize()
方法:
/* Keep in mind #resize() takes the width and height parameters as px values, not dp */
Picasso.with(this).load(newPath).resize(1280, 1280).centerCrop().into(addPhoto, new CallBack() {
// implementation
});
或使用
fit()
方法:Picasso.with(this).load(newPath).fit().into(addPhoto, new Callback() {
// implementation
});
您可以使用
Target
从毕加索获取Bitmap
(此方法仅适用于resize()
,不适用于fit()
):Picasso.with(this).load(newPath).resize(1280, 1280).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
// TODO: use bitmap for other purposes
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
imageView.setImageDrawable(placeHolderDrawable);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
imageView.setImageDrawable(errorDrawable);
}
});
要实际压缩
Bitmap
,可以使用ByteArrayOutputStream
和Bitmap.compress()
方法:ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
如果要进行无损压缩(不损失图像质量),请将第二个参数设置为100。否则,可以将该参数设置为0到100之间的任何值,这将导致文件较小,并且图像质量也会下降。
然后,您可以使用此
ByteArrayOutputStream
将数据发送到服务器。1从技术上讲,您要尝试执行的操作不是data compression,而是缩小图像的比例。但是,有时两者确实是协同工作的。
关于android - 如何正确压缩此位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39999202/