【发布时间】:2017-01-12 07:30:32
【问题描述】:
我是 Android 新手,正在从事一个项目,我必须使用带有叠加图像的自定义相机来捕捉图像。
我可以使用覆盖(组合图像)捕获图像,但问题是在保存新的组合图像后覆盖图像方向发生变化,即覆盖图像主要设置为纵向模式,但保存后显示为横向模式.
我已经搜索了很多 如何更改 Drawable Image 的方向,但任何地方的方向更改都是针对位图图像。如果我将可绘制图像更改为位图并更改旋转它不会显示任何叠加图像.我不知道代码有什么问题。 我的代码如下。
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);//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);
提前感谢您的帮助。
【问题讨论】: