gpt4 book ai didi

android - 如何在不模糊的情况下或使用 picasso 调整图像大小

转载 作者:行者123 更新时间:2023-11-29 01:28:33 25 4
gpt4 key购买 nike

我需要帮助改进我的代码。

我在做什么:主要 Activity 中有一个按钮,单击时,用户选择图像,之后,图像通过 Intent 传递给另一个 Activity (add_image.java)和在 ImageView 中显示,然后我将图像发送到服务器。

我的问题:1)我想要将路径图像发送到第二 Intent 然后将其转换为图像的最佳方式
2)然后在不损失很多质量的情况下尽可能多地压缩它。image现在的大小是 376kb 。所以在我的应用程序中我会显示几张图片,所以在这样的尺寸下它会消耗时间和互联网来加载。(我正在使用 picasso 并且 fit() 没有减小尺寸。)

这是我的代码:

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

//file name
filePath = data.getData();
try {
// Bundle extras2 = data.getExtras();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Intent i = new Intent(this, AddImage.class);
i.putExtra("image", imageInByte);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } } }

我在这里收到图片

     byte[] byteArray = getIntent().getByteArrayExtra("image");
encodedImage = Base64.encodeToString(byteArray, Base64);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);

最佳答案

尝试下面的代码,您可能需要根据您的要求修改一些参数:

创建 MainActivity 如下:

public class MainActivity extends Activity implements OnClickListener {
private final int REQUEST_IMAGE_GALLERY = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
pickImageFromGallery();
break;

default:
break;
}
}

private void pickImageFromGallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"),
REQUEST_IMAGE_GALLERY);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode != Activity.RESULT_OK) {
return;
}

if (requestCode == REQUEST_IMAGE_GALLERY) {
Uri selectedImageUri = data.getData();

String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

Log.e("PATH", "" + picturePath);
Intent intent = new Intent(this, AddImage.class);
intent.putExtra("PATH", picturePath);
startActivity(intent);

}
}
}

现在创建 activity_main.xml 如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ChooseButton" />

</LinearLayout>

然后我们需要按如下方式创建 AddImage Activity :

public class AddImage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_image);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
if (getIntent() != null) {
String path = getIntent().getStringExtra("PATH");
Log.e("PATHR", "" + path);
new BitmapWorkerTask(imageView).execute(path);
}

}

private boolean isNeedToBeScaled(String path) {
File file = new File(path);

if (file.length() > (1024 * 1024) && isExist(path)) {
Log.e("SCALEIMAGE", "SACLE");
return true;
}
return false;
}

private boolean isExist(String path) {
File file = new File(path);
return file.exists();
}

private Bitmap getScaledImage(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
int[] newWH = new int[2];
newWH[0] = srcWidth / 2;
newWH[1] = (newWH[0] * srcHeight) / srcWidth;

int inSampleSize = 2;
while (srcWidth / 2 >= newWH[0]) {
srcWidth /= 2;
srcHeight /= 2;
inSampleSize *= 2;

options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = inSampleSize;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path, options);

return sampledSrcBitmap;
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;

public BitmapWorkerTask(ImageView mImageView) {
imageViewReference = new WeakReference<ImageView>(mImageView);
}

@Override
protected Bitmap doInBackground(String... params) {
Bitmap scaled = null;
if (isNeedToBeScaled(params[0])) {
Bitmap d;

d = getScaledImage(params[0]);
int nh = (int) (d.getHeight() * (512.0 / d.getWidth()));
scaled = Bitmap.createScaledBitmap(d, 512, nh, true);

} else {
scaled = BitmapFactory.decodeFile(params[0], null);
}
return scaled;

}

@Override
protected void onPostExecute(Bitmap result) {

if (imageViewReference != null && result != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
imageView.setVisibility(View.VISIBLE);

}
}
}

}
}

关于android - 如何在不模糊的情况下或使用 picasso 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551633/

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