我正在一个从Android相机捕获图像的项目中。一切正常。
但是现在面临的一个问题是,预览显示效果很好,但是在捕获图像后,两者都不是100%相同。捕获图像后,有时图像会被拉伸或挤压。
CameraPreview类:
import pandas as pd
temp=u"""DATE,DTB3
8/4/2014,0.0004
8/5/2014,0.0003
8/6/2014,0.0003"""
#after testing replace 'pd.compat.StringIO(temp)' to FRED_file
T1 = pd.read_csv(pd.compat.StringIO(temp),
dtype= {'DTB3':'str'},
parse_dates=['DATE'],
index_col='DATE'
)
print (T1)
DTB3
DATE
2014-08-04 0.0004
2014-08-05 0.0003
2014-08-06 0.0003
print (T1.index)
DatetimeIndex(['2014-08-04', '2014-08-05', '2014-08-06'],
dtype='datetime64[ns]', name='DATE', freq=None)
下面是xml布局,其中在FrameLayout中使用了LinearLayout(@ + id / cameraFrame)下的摄像机预览。
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Context mContext;
float mDist = 0;
public CameraPreview(Context context, Camera camera) {
super(context);
mContext = context;
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
// create the surface and start camera preview
if (mCamera == null) {
Camera.Parameters params = mCamera.getParameters();
if (params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
mCamera.setParameters(params);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
} catch (IOException e) {
Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void refreshCamera(Camera camera) {
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
setCamera(camera);
// TODO: don't hardcode cameraId '0' here... figure this out later.
//setCameraDisplayOrientation(mContext, Camera.CameraInfo.CAMERA_FACING_FRONT, mCamera);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (Exception e) {
Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
}
}
public static void setCameraDisplayOrientation(Context context, int cameraId, Camera camera) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = wm.getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
// Compensate for the mirror image.
result = (360 - result) % 360;
} else {
// Back-facing camera.
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
refreshCamera(mCamera);
}
public void setCamera(Camera camera) {
//method to set a camera instance
mCamera = camera;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
// mCamera.release();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Get the pointer ID
Camera.Parameters params = mCamera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
// handle multi-touch events
if (action == MotionEvent.ACTION_POINTER_DOWN) {
mDist = getFingerSpacing(event);
} else if (action == MotionEvent.ACTION_MOVE
&& params.isZoomSupported()) {
mCamera.cancelAutoFocus();
handleZoom(event, params);
}
} else {
// handle single touch events
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = getFingerSpacing(event);
if (newDist > mDist) {
// zoom in
if (zoom < maxZoom)
zoom++;
} else if (newDist < mDist) {
// zoom out
if (zoom > 0)
zoom--;
}
mDist = newDist;
params.setZoom(zoom);
mCamera.setParameters(params);
}
public void handleFocus(MotionEvent event, Camera.Parameters params) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null
&& supportedFocusModes
.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
}
}
/** Determine the space between the first two fingers */
private float getFingerSpacing(MotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float)Math.sqrt(x * x + y * y);
}
}
我已经在Google驱动器中共享了Activity类,Camera Preview类和xml文件。 https://drive.google.com/drive/folders/1WETYNhUZiTZOOChzEynRC-cEB6RsYrN5?usp=sharing
在捕获图像之前:
捕获图像后:
您可以在捕获图像后在左侧看到字母“ F”。但是字母“ F”不在预览中。捕获图像后,顶部栏的“ hp”徽标消失了,但是预览中出现了“ hp”徽标。
我尝试了一些来自堆栈溢出的解决方案,但它不适合我的情况。当我尝试Android Camera Preview Stretched解决方案时,没有显示摄像机预览,因此我无法确定它是否已解决。
请让我知道如何解决此问题?
答案 0 :(得分:0)
拍摄图像后,需要裁剪位图。放
public static void cropToJpeg2(final byte[] jpeg, final AspectRatio targetRatio, final int jpegCompression, final CameraUtils.BitmapCallback callback) {
final Handler ui = new Handler();
WorkerHandler.run(new Runnable() {
@Override
public void run() {
Bitmap image = decodeBitmap(jpeg, Integer.MAX_VALUE, Integer.MAX_VALUE);
Rect cropRect = computeCrop(image.getWidth(), image.getHeight(), targetRatio);
final Bitmap crop = Bitmap.createBitmap(image, cropRect.left, cropRect.top, cropRect.width(), cropRect.height());
image.recycle();
ByteArrayOutputStream out = new ByteArrayOutputStream();
crop.compress(Bitmap.CompressFormat.JPEG, jpegCompression, out);
ui.post(new Runnable() {
@Override
public void run() {
callback.onBitmapReady(crop);
}
});
}
});
}
计算作物功能
private static Rect computeCrop(int currentWidth, int currentHeight, AspectRatio targetRatio) {
AspectRatio currentRatio = AspectRatio.of(currentWidth, currentHeight);
logger.e("CropHelper", "computeCrop: currentRatio " + currentRatio);
logger.e("CropHelper", "computeCrop: targetRatio " + targetRatio);
int x, y, width, height;
if (currentRatio.toFloat() > targetRatio.toFloat()) {
height = currentHeight;
width = (int) (height * targetRatio.toFloat());
y = 0;
x = (currentWidth - width) / 2;
} else {
width = currentWidth;
height = (int) (width / targetRatio.toFloat());
// y = (currentHeight - height) / 2;
y = 0; //change above line for crop exact image from camera (remove heading).
x = 0;
}
return new Rect(x, y, x + width, y + height);
}
注意:-
decodeBitmap
在为旋转目的进行Exif计算后,函数返回位图。