我是Android新手,正在开发一个项目,我必须使用带有叠加图像的自定义相机捕捉图像。
我可以使用叠加(合成图像)捕捉图像,但问题是保存新合成图像后叠加图像方向发生变化,即叠加图像主要设置为纵向模式但保存后以横向模式显示
我已经搜索了很多如何更改可绘制图像的方向,但是到处方向更改都是针对位图图像。如果我将可绘制图像更改为位图并更改旋转,则不会显示任何叠加图像.i不知道代码有什么问题。 我的代码如下。
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);
}`
以下是摄像机imageView和叠加图像的主要活动中的布局调用:
surfaceView = (SurfaceView) findViewById(R.id.cameraView);//for camera view
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());//for overlay view inflated over the 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);
在此先感谢您的帮助。
答案 0 :(得分:2)
经过对该主题的一些研究后,我发现不可能旋转(没有动画)或改变图像的图像方向。 我查看文档Bitmap& View。 View文档声明:
void setDrawingCacheEnabled(已启用布尔值) 启用或禁用绘图缓存。启用绘图缓存后,下一次调用getDrawingCache()或buildDrawingCache()将在位图中绘制视图。启用缓存时,调用draw(android.graphics.Canvas)不会从缓存中绘制。
之后我添加了以下几行,以便通过在onPictureTaken()方法中使用 setDrawaingCacheEnabled(),以与原始方向相同的方向从drawable访问图像。
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);