【发布时间】:2019-02-14 14:20:45
【问题描述】:
我想通过单击按钮仅捕获矩形中的部分并将其保存在存储中See Image Here我正在创建一个应用程序,背景是相机预览,中心存在一个矩形(矩形由在矩形周围创建四个布局并将它们的背景颜色设置为部分透明,这样当我单击捕获图像按钮时,它看起来就像添加了一个叠加层,它捕获了整个屏幕预览的图像,但我只想要存在的部分图像在矩形中,这是我尝试过的,
captuteimageonpro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
camera.takePicture(null, null, mPictureCallback);
}
});
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int start_width = (int) (width * 0.15);
int start_height = (int) (height * 0.16);
int end_width = start_width + (int) (width * 0.70);
int end_height = start_height + (int) (height * 0.52);
//the decimal values in above lines are the percentages of the rectangle position relative to screen
int no_pixels = (end_width - start_width) * (end_height - start_height);
int[] pixels = new int[no_pixels];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.getPixels(pixels, 0, (end_width - start_width), start_width, start_height, (end_width - start_width), (end_height - start_height));
bitmap = Bitmap.createBitmap(pixels, 0, (end_width - start_width), (end_width - start_width), (end_height - start_height), Bitmap.Config.ARGB_8888);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bitmap.recycle();
File picture_file = getOutputMediaFile();
if(picture_file == null)
{
return;
}
else {
try {
FileOutputStream fos = new FileOutputStream(picture_file);
fos.write(byteArray);
fos.close();
camera.startPreview();
}catch (FileNotFoundException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
};
private File getOutputMediaFile()
{
String state = Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED))
{
return null;
}
else
{
File folder_gui = new File(Environment.getExternalStorageDirectory()+ File.separator+"GUI");
if(!folder_gui.exists())
{
folder_gui.mkdirs();
}
File outputFile = new File(folder_gui,"temp.jpg");
return outputFile;
}
我面临的问题是,在某些手机上,在该部分捕获的图像的清晰度非常差,就像模糊一样,在某些手机中,图像是旋转保存的,有人可以帮我吗?我应该做的任何改变,或者有没有其他方法可以有效地做到这一点。
我不想手动裁剪图像,只需单击按钮后,矩形中存在的相机预览部分应该保存在存储中。
【问题讨论】:
-
@Srijay 请让我们知道所提供的答案是否适合您
-
@ThomasRichter 感谢您的回答,但它没有解决我的问题,请检查我添加到您的回答中的评论
标签: android bitmap android-camera