【发布时间】:2018-02-06 12:04:54
【问题描述】:
我正在制作一个基于相机的应用程序,我在相机上放置了一个矩形视图。
当我使用new Camera.PictureCallback() 捕获图像时,我裁剪了该图像,以便获得矩形的一部分。
嗯,它工作正常。
现在我实现了View.OnTouchListener 并使用它使形状可移动。
因此,我需要使用用户的最终选择来捕获图像,例如他们放置矩形的位置。
Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440
float scale = 1280 / 1000;
int left = (int) (scale * (imageOriginal.getWidth() - 250) / 2);
int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);
int width = (int) (scale * 750);
int height = (int) (scale * 616);
Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);
这是我用来裁剪图像的方法。这些值是硬线来找到确切位置的。 现在我需要改变矩形的顶部、底部、高度、宽度的值。
//我用来绘制那个矩形的customView
public class CustomView extends View {
private Paint paint = new Paint();
public CustomView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) { // Override the onDraw() Method
super.onDraw(canvas);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
//center
int x0 = canvas.getWidth()/2;
int y0 = canvas.getHeight()/2;
int dx = canvas.getHeight()/3;
int dy = canvas.getHeight()/3;
//draw guide box
canvas.drawRect(x0-dx, y0-dy, x0+dx, y0+dy, paint);
}
}
//我的图片回调代码
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// Replacing the button after a photho was taken.
// File name of the image that we just took.
fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()).toString() + ".jpg";
// Creating the directory where to save the image. Sadly in older
// version of Android we can not get the Media catalog name
File mkDir = new File(sdRoot, dir);
mkDir.mkdirs();
// Main file where to save the data that we recive from the camera
File pictureFile = new File(sdRoot, dir + fileName);
// Cropping image with the corresponding co-ordinates and save in to a file
try {
Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); // 2560×1440
float scale = 1280 / 1000;
int left = (int) (scale * (imageOriginal.getWidth() - 250) / 2);
int top = (int) (scale * (imageOriginal.getHeight() - 616) / 2);
int width = (int) (scale * 750);
int height = (int) (scale * 616);
Bitmap imageConverted = Bitmap.createBitmap(imageOriginal, left, top, width, height, null, false);
FileOutputStream purge = new FileOutputStream(pictureFile);
imageConverted.compress(Bitmap.CompressFormat.JPEG, 100, purge);
purge.flush();
purge.close();
} catch (FileNotFoundException e) {
Log.d("DG_DEBUG", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("DG_DEBUG", "Error accessing file: " + e.getMessage());
}
// Adding Exif data for the orientation.
try {
ProjectManager.getInstance().settings.IMAGE_LOCATION = "/sdcard/" + dir + fileName;
exif = new ExifInterface(ProjectManager.getInstance().settings.IMAGE_LOCATION);
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation);
exif.saveAttributes();
mView.saveImage(dir + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
};
【问题讨论】:
-
用户在哪里放置绿色矩形,我只需要捕获或保存该部分。
-
绿色矩形位于相机视图上方。那个背景是一个正在运行的相机。
-
没有回复??为什么??
-
嗨@Varun Chandran .. 到目前为止你有没有找到任何解决方案?
-
@VibhorBhardwaj 下面的答案给出了类似的输出,但对我的问题并不完美。
标签: android dynamic view android-camera android-canvas