Android 如何获得完全清晰的图像?
我尝试从sdcard camera文件夹加载图像,加载后我使用缩放位图创建了位图,因为我只想显示图像的某些部分,所以我尝试使用createBitmap()创建新位图,但它仍然变得模糊,如何解决此问题Android 如何获得完全清晰的图像?,android,bitmap,bitmapimage,android-bitmap,Android,Bitmap,Bitmapimage,Android Bitmap,我尝试从sdcard camera文件夹加载图像,加载后我使用缩放位图创建了位图,因为我只想显示图像的某些部分,所以我尝试使用createBitmap()创建新位图,但它仍然变得模糊,如何解决此问题 File[] files = targetDirector.listFiles(); for (File file : files) { String path = file.getAbsolutePath();
File[] files = targetDirector.listFiles();
for (File file : files) {
String path = file.getAbsolutePath();
itemList.add(path);
if (myBitmap != null) {
myBitmap = null;
}
myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
System.out.println("thumbnails1 width: " + myBitmap.getWidth());
System.out.println("thumbnails1 height: " + myBitmap.getHeight());
myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150, false);
ExifInterface exif;
try {
exif = new ExifInterface(file.getAbsolutePath());
String orientString = exif
.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer
.parseInt(orientString)
: ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
System.out.println("photopotart");
rotationAngle = 90;
System.out.println("myBitmap" + myBitmap.getWidth());
System.out.println("myBitmap" + myBitmap.getHeight());
myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150,
false);
Matrix matrix = new Matrix();
matrix.postRotate(90);
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
150, 150, matrix,
true);
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, 150, 100 );
myBitmap = highlightImage(myBitmap);
photsList.add(myBitmap);
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
System.out.println("180 angle");
rotationAngle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
System.out.println("other 270");
rotationAngle = 270;
} else {
System.out.println("photolandscape");
myBitmap = Bitmap.createScaledBitmap(myBitmap, 150, 150,
false);
myBitmap = Bitmap.createBitmap(myBitmap, 30, 0, 120, 150);
photsList.add(myBitmap);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我的xml文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/img"
android:layout_width="150dip"
android:layout_height="150dip" />
</LinearLayout>
使用以下方法解码文件路径
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 2;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(filePath, o2); // this bmp object of Bitmap is global and you can set it to your ImageView.
}
到
您可以根据您的要求设置缩放变量,这样您可以获得非常清晰的图像。您好,谢谢您的回复。使用此选项后,与前一个选项相比,效果稍好一些,但出现的问题是照片上出现了阴影。您可以根据需要增加所需的大小和缩放变量。因此,您可以获得更好的输出。目前我正在使用s3 mini和nexus4,那么我可以为所需大小提供多少?您可以为其提供2048并将其缩放到3。我可以知道缩放和所需大小的用法吗?
myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
decodeFile(file.getAbsolutePath());