gpt4 book ai didi

java - 上传前的方形位图

转载 作者:行者123 更新时间:2023-12-02 11:37:00 24 4
gpt4 key购买 nike

你好,我有以下代码用于从画廊 Activity 中获取图像,我希望在上传之前将图像平方为特定尺寸(例如 400*400),同时保持其纵横比,以便所有上传的图像具有相同的大小。有什么想法请提出来

@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 "";
}

最佳答案

添加此代码以在上传图像之前调整图像大小

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 "";
}

关于java - 上传前的方形位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48864799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com