我正在使用相机API拍照我必须根据我的图像视图大小打开不同尺寸的相机。我正在关注我们在Android sdk / sample / adroid-18中以“ApiDemo”名称进入的示例项目我已更改的内容未在setcontentview上设置相机。我已将相机设置在Frame Layout上。起初我的相机预览是starched所以我得到了相机OptimalPreviewSize并使FrameLayout参数宽度和高度为wrap -content.Now相机预览小于ImageView(我想要的大小)。如果我将FrameLayout参数的大小设置为匹配父级,则相机视图会拉伸。如何解决此问题。
找到此链接以获取更多规范。 Android camera preview look strange
更新
我的相机预览尺寸很好现在我使用on Layout方法我的想法是我有更大的布局然后我的ImageView和现在的相机预览看起来很好。 现在我面临的问题是为这个设置适当大小的图像我必须像我的ImageView中心裁剪和缩放相同的尺寸。这是我通过TakePicture方法得到的图像并保存在sdcard中。
为此我使用这种方法: -
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_crop我不能在我的情况下使用它,并且不想向用户显示裁剪框,这个所有过程都不应该在UI上显示。
更新
我根据我的imageView尺寸
使用来自中心和比例的裁剪图像的打击方法Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);
但我得到的位图与FrameLayout上显示的相机预览看起来并不相同。因为相机预览很大。我认为这些代码裁剪了大面积。 我试图减小宽度并改变高度,但没有获得我想要的相同裁剪图像。
在图片裁剪后,我在FrameLayout上自动设置了最后一个图像框。我们可以从Frame Layout获得该设置帧吗?这怎么可能?
以下是How to retrieve the visible part of a SurfaceView in Android这样的问题,任何人都有解决方案。
我希望通过这一行ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);
来实现这一目标,并且通过这一行,我得到的src就像图中所描述的那样。
这一行要改变什么????
答案 0 :(得分:0)
@Akanksha请使用下面的代码,您只需要传递已保存图像的路径,以及我们的imageview的高度和宽度。这段代码非常适合我。
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;
}
}
我在异步任务中调用此方法,因为它可能需要太多的UImemory和时间 我就是这样称呼的。
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);
以上代码是根据我的要求编写的,您可以根据自己的要求进行修改。
答案 1 :(得分:0)
中心裁剪图像可能对您有帮助。
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;
}