gpt4 book ai didi

java - Android 保存的图像质量低

转载 作者:行者123 更新时间:2023-11-29 21:59:41 25 4
gpt4 key购买 nike

我正在通过以下代码拍照并保存到 SD 卡,但它生成的图片质量很低,即使是 100% 质量也非常小。也许 bitmap.compress 不是正确的方法(或者根本不是位图?!)

这是我的代码:

public class TakePhoto extends Activity {

ImageView iv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);


iv = (ImageView) findViewById(R.id.imageView1);

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);

}
});
}

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

Bitmap bm = (Bitmap) data.getExtras().get("data");
Random generator = new Random();
String randFileName = String.valueOf (generator.nextInt(965) + 32);
String both = "/mnt/extSdCard/DirectEnquiries/"+ randFileName + ".jpg";
File imageFile = new File(both);

writeBitmapToMemory(imageFile, bm);
iv.setImageBitmap(bm);

}

public void writeBitmapToMemory(File file, Bitmap bitmap) {
FileOutputStream fos;

try {
Log.e("Tom", "Starting take stream");
fos = new FileOutputStream(file);
Log.e("Tom", "Got stream");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Log.e("Tom", "Saved Image");
fos.close();

}
catch (FileNotFoundException e) {
e.printStackTrace();


}
catch (IOException e) {
e.printStackTrace();


}

}

}

最佳答案

请调用以下函数从相机捕获图像。

 private final static String FOLDER_NAME = "YourAppName/Image/";
private Uri selectedImageUri = null;

public void startCamera()
{
File photo = null;
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
photo = new File(android.os.Environment.getExternalStorageDirectory(), FOLDER_NAME+File.separator+timeStamp+".png");
}
else
{
photo = new File(getCacheDir(), FOLDER_NAME+File.separator+timeStamp+".png");
}
if (photo != null)
{
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
selectedImageUri = Uri.fromFile(photo);
startActivityForResult(intent, CAPTURE_IMAGE_CALLBACK);
}
}

您可以在 selectedImageUri 变量中获取图片 Uri。 (图片存储在Sdcard中)

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
switch (requestCode)
{
case CAPTURE_IMAGE_CALLBACK:

break;
}
}

关于java - Android 保存的图像质量低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12277401/

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