Android 在ImageView上设置适当大小的中心裁剪图像
我正在使用摄像头API拍照,我必须根据我的图像视图大小打开不同大小的摄像头。我正在关注Android sdk/sample/adroid-18中名为“ApiDemo”的示例项目。我所做的更改是在setcontentview上不设置摄像头。我已将相机设置为框架布局。起初,我的相机预览已经开始,所以我得到了相机最佳预览尺寸,并将FrameLayout参数width和height设置为wrap-content。现在相机预览比ImageView(我想要的尺寸)小。如果我将FrameLayout参数的大小设置为匹配父对象,则相机视图是拉伸的。如何解决此问题 查找此链接以了解更多规范 更新 我的相机预览尺寸很好,现在我使用的是布局上的方法,想法是我有更大的布局,然后我的ImageView,现在相机预览看起来不错。 现在我面临的问题是设置适当大小的图像,为此,我必须在像ImageView一样的视图中居中裁剪和缩放相同大小的图像。这张图像我通过拍摄方法获得并保存在SD卡中 为此,我使用以下方法:-Android 在ImageView上设置适当大小的中心裁剪图像,android,camera,center,crop,android-framelayout,Android,Camera,Center,Crop,Android Framelayout,我正在使用摄像头API拍照,我必须根据我的图像视图大小打开不同大小的摄像头。我正在关注Android sdk/sample/adroid-18中名为“ApiDemo”的示例项目。我所做的更改是在setcontentview上不设置摄像头。我已将相机设置为框架布局。起初,我的相机预览已经开始,所以我得到了相机最佳预览尺寸,并将FrameLayout参数width和height设置为wrap-content。现在相机预览比ImageView(我想要的尺寸)小。如果我将FrameLayout参数的大小
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left+50, top, left + scaledWidth, top + scaledHeight+50);
// RectF targetRect = new RectF(0, 0, newWidth, newHeight/2);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
但结果图像质量不好。从顶部和底部切割高度角,结果图像质量不好。像素拉伸
不要告诉我使用scaleType=Center\u crop我不能在我的情况下使用它,并且不想向用户显示裁剪帧,这所有过程都不应该显示在UI上
更新
根据imageView的大小,我使用了从中心和比例裁剪图像的吹扫方法
Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);
但是我得到的位图与FrameLayout上显示的相机预览图不一样。因为相机预览很大。我认为这些代码裁剪了很大的区域。
我试图缩小宽度和改变高度,但没有得到我想要的相同比例的裁剪图像
在FrameLayout上自动裁剪最后一个图像帧后,我还有一个想法。我们可以从框架布局中获得设置框架吗。这怎么可能
这里有一个像这样的问题,有人能解决吗
我想通过这一行ThumbnailUtils.extractThumbnail(source,newWidth,newHeight)来实现这一点代码>通过这一行,我得到了图中描述的类似src的图像
这行到底要换什么
@Akanksha请使用下面的代码,您只需传递保存图像的路径,以及图像视图的高度和宽度。这个代码对我来说非常有效
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageHandler {
/**
* Decode and sample down a bitmap from a file to the requested width and
* height.
*
* @param filename
* The full path of the file to decode
* @param reqWidth
* The requested width of the resulting bitmap
* @param reqHeight
* The requested height of the resulting bitmap
* @return A bitmap sampled down from the original with the same aspect
* ratio and dimensions that are equal to or greater than the
* requested width and height
*/
public static Bitmap decodeSampledBitmapFromFile(String filename,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filename, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
// This offers some additional logic in case the image has a
// strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might
// still
// end up being too large to fit comfortably in memory, so we
// should
// be more aggressive with sample down the image (=larger
// inSampleSize).
final float totalPixels = width * height;
// Anything more than 2x the requested pixels we'll sample down
// further.
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
}
我在异步任务中调用此方法,因为它可能占用太多的内存和时间
我这样称呼它
class Asyncing extends AsyncTask {
private int reqWidth;
private int reqHeight;
private ImageView iv;
private String fileName;
private ProgressDialog pd;
public Asyncing(int reqWidth, int reqHeight, ImageView iv,
String fileName) {
super();
this.reqWidth = reqWidth;
this.reqHeight = reqHeight;
this.fileName = fileName;
this.iv = iv;
}
@Override
protected Bitmap doInBackground(String... params) {
return ImageHandler.decodeSampledBitmapFromFile(params[0],
reqWidth, reqHeight);
}
@Override
protected void onPostExecute(Bitmap result) {
iv.setImageBitmap(result);
if (pd.isShowing()) {
pd.setMessage(getString(R.string.completed));
pd.dismiss();
}
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(CustomerDetailsActivity.this, "",
getString(R.string.processing_signature));
super.onPreExecute();
}
}
这就是调用asynctask的方式
signedImagePath = data.getExtras().getString("imagePath");
new Asyncing(signature_img.getWidth(), signature_img.getHeight(),
signature_img, "spenTest.png").execute(signedImagePath);
以上代码是根据我的要求编写的,您可以根据自己的要求进行修改 居中裁剪图像可能有助于实现这一点
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
发布你想要的图片快照!请检查更新后的简单解决方案:此处signedImagePath是图像文件的路径,spentest.png是图像文件的名称。