gpt4 book ai didi

android - 从可绘制对象保存图像后覆盖图像方向发生变化

转载 作者:搜寻专家 更新时间:2023-11-01 09:38:01 26 4
gpt4 key购买 nike

我是 Android 的新手,正在从事一个项目,我必须使用带有叠加图像的自定义相机来捕捉图像。

我可以使用叠加层(组合图像)捕获图像,但问题是在保存新的组合图像后叠加图像方向发生变化,即叠加图像主要设置为纵向模式,但在保存后显示为横向模式.

我搜索了很多如何更改可绘制图像的方向,但到处都是位图图像的方向变化。如果我将可绘制图像更改为位图并更改旋转,它不会显示任何叠加图像.我不知道代码有什么问题。我的代码如下。

public void onPictureTaken(byte[] arg0, Camera camera) {
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
int wid = cameraBitmap.getWidth();
int hgt = cameraBitmap.getHeight();
Bitmap newImage = Bitmap.createBitmap(wid,hgt,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
// Drawable drawable = getResources().getDrawable(R.drawable.frame);
// drawable.setBounds(0,0,drawable.getIntrinsicWidth()*1/2, drawable.getIntrinsicHeight()*1/2);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.frame);
Matrix matrix = new Matrix();
matrix.postRotate(270);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, 100, 200, true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
Drawable drawable = new BitmapDrawable(getResources(), rotatedBitmap);
drawable.draw(canvas);
File storagePath = new File(Environment.getExternalStorageDirectory() + "/Photo/");
storagePath.mkdirs();
File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");
try
{
FileOutputStream out = new FileOutputStream(myImage);
ExifInterface exif = new ExifInterface(cameraBitmap.toString());
Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")) {
newImage = rotate(newImage, 90);
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")) {
newImage = rotate(newImage, 270);
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")) {
newImage = rotate(newImage, 180);
} else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")) {
newImage = rotate(newImage, 90);}
newImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
camera.startPreview();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + myImage.getAbsolutePath()), "image/*");
startActivity(intent);
newImage.recycle();
newImage = null;
out.flush();
out.close();
}
catch(FileNotFoundException e)
{
Log.d("In Saving File", e + "");
}
catch(IOException e)
{
Log.d("In Saving File", e + "");
}
}};

public Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
// mtx.postRotate(degree);
mtx.setRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}`

以下是在 mainactivity 中为相机 imageView 和叠加图像调用的布局:

surfaceView = (SurfaceView) findViewById(R.id.cameraView);//用于相机 View
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(这个);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());//用于在 surfaceView 上膨胀的覆盖 View
查看 view = controlInflater.inflate(R.layout.overlay, null);
view.setSaveEnabled(true);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(view, layoutParamsControl);
在此先感谢您的帮助。

最佳答案

在对这个主题进行一些研究之后,我发现无法旋转(没有动画)或更改可绘制图像的图像方向。我浏览文档 Bitmap & View .View 文档指出:

void setDrawingCacheEnabled(boolean enabled) Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled.

之后,我添加以下行,以便通过在 onPictureTaken() 方法中使用 setDrawingCacheEnabled() 从与原始方向相同的可绘制对象访问图像。

 view.setDrawingCacheEnabled(true);
Bitmap viewCapture = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

除了在输出屏幕中设置可绘制图像尺寸外,还需要使用 setBounds 方法(我在问题中忽略了它)

drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 1/2, drawable.getIntrinsicHeight() * 1/2);

关于android - 从可绘制对象保存图像后覆盖图像方向发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41607421/

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