[英]Square bitmap before uploading
hello i have the following code for getting image from gallery activity and i want the image to be squared to particular dimension say 400*400 before it is uploaded while maintaining its aspect ratio so that all uploaded images be the same size. 您好,我有以下代码用于从图库活动中获取图像,并且我希望图像在上传之前保持特定的尺寸,例如400 * 400,同时保持其长宽比,以使所有上传的图像大小相同。 any ideas please
任何想法请
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
assert selectedImage != null;
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = findViewById(R.id.imagebutton);
int width = imgView.getWidth();
int height = imgView.getHeight();
Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imgPath),width, height, false);
imgView.setImageBitmap(bitmap);
// Get the Image's file name
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// Put file name in Async Http Post Param which will used in Php web app
params.put("filename", fileName);
} else {
Toast.makeText(this, R.string.noimageselected,
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, R.string.erroroccurred, Toast.LENGTH_LONG)
.show();
}
}
@Override
protected String doInBackground(Void... params) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
return "";
}
Add this code for resize your image before uploading image 添加此代码以在上传图片之前调整图片大小
int newWidth=400;
int newHeight=400;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap , newWidth, newHeight, true);
@Override
protected String doInBackground(Void... params) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
//Add these lines
int newWidth=400;
int newHeight=400;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap , newWidth, newHeight, true);
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
return "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.