Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓摄像头在纵向拍摄时拍摄横向照片_Java_Android_Camera_Orientation_Landscape - Fatal编程技术网

Java 安卓摄像头在纵向拍摄时拍摄横向照片

Java 安卓摄像头在纵向拍摄时拍摄横向照片,java,android,camera,orientation,landscape,Java,Android,Camera,Orientation,Landscape,我采用了来自美国的代码 基本上,我在相机的中间添加了一个包围框来保存图片的一部分。当相机处于横向模式时,它可以完美地拍摄照片。我打电话给警察 mCamera.setDisplayOrientation(90); 方法和预览以纵向模式显示,并且边界框也正确对齐,但拍摄照片时,它会拍摄与边界框成90度角的区域 public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, acquire

我采用了来自美国的代码

基本上,我在相机的中间添加了一个包围框来保存图片的一部分。当相机处于横向模式时,它可以完美地拍摄照片。我打电话给警察

mCamera.setDisplayOrientation(90);
方法和预览以纵向模式显示,并且边界框也正确对齐,但拍摄照片时,它会拍摄与边界框成90度角的区域

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where to draw.
    try {
        mCamera = Camera.open();
        mCamera.setDisplayOrientation(90);
    }
    catch (RuntimeException exception) {
        //Log.i(TAG, "Exception on Camera.open(): " + exception.toString());
        Toast.makeText(getContext(), "Camera broken, quitting :(",Toast.LENGTH_LONG).show();
        // TODO: exit program
    }
这种方法拍摄图片

public Bitmap getPic(int x, int y, int width, int height) {
    System.gc();
    Bitmap b = null;
    Size s = mParameters.getPreviewSize();

    YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width, s.height, null);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
    b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, outStream.size()); // decode JPG
    if (b != null) {
        //Log.i(TAG, "getPic() WxH:" + b.getWidth() + "x" + b.getHeight());
    } else {
        //Log.i(TAG, "getPic(): Bitmap is null..");
    }
    yuvimage = null;
    outStream = null;
    System.gc();
    return b;
}
我从按钮的onclick监听器的摄影机活动中调用它

// This method takes the preview image, grabs the rectangular
// part of the image selected by the bounding box and saves it.
// A thread is needed to save the picture so not to hold the UI thread.
private OnClickListener previewListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mAutoFocus){
            mAutoFocus = false;
            //mPreview.setCameraFocus(myAutoFocusCallback);
            Wait.oneSec();
            Thread tGetPic = new Thread( new Runnable() {
                public void run() {
                    Double[] ratio = getRatio();
                    int left = (int) (ratio[1]*(double)mView.getmLeftTopPosX());
                    // 0 is height
                    int top = (int) (ratio[0]*(double)mView.getmLeftTopPosY());
                    int right = (int)(ratio[1]*(double)mView.getmRightBottomPosX());
                    int bottom = (int)(ratio[0]*(double)mView.getmRightBottomPosY());
                    Message msg = Message.obtain();
                    try {
                        savePhoto(mPreview.getPic(left,top,right,bottom));
                        mAutoFocus = true;
                        msg.arg1 = 1;
                    }
                    catch (Exception e) {
                        Log.w("SAV_JPG",e.getMessage());
                        msg.arg1 = -1;
                    }
                    saveImageHandler.sendMessage(msg);
                }
            });
            tGetPic.start();

        }
        boolean pressed = false;
        if (!mTakePicture.isPressed()){
            pressed = true;
        }
    }
};
请帮忙

谢谢, 努鲁尔

编辑:

下面是该场景的说明。边界框区域是我想要捕捉的部分。下面是如何计算边界框的边界

Display display = wm.getDefaultDisplay();
    Point size = new Point();
    int width = display.getWidth();
    int height = display.getHeight();
    mLeftTopPosY = Math.round(height / 2) - 100;
    mLeftTopPosX = Math.round(width / 2) - 250;
    mRightTopPosY = Math.round(height / 2) - 100;
    mRightTopPosX = Math.round(width / 2) + 250;
    mLeftBottomPosY = Math.round(height / 2) + 100;
    mLeftBottomPosX = Math.round(width / 2) - 250;
    mRightBottomPosY = Math.round(height / 2) + 100;
    mRightBottomPosX = Math.round(width / 2) + 250;

            mCenter = mLeftTopIcon.getMinimumHeight()/2;
    mLeftTopIcon.setBounds((int)mLeftTopPosX, (int)mLeftTopPosY,
            mLeftTopIcon.getIntrinsicWidth()+(int)mLeftTopPosX,
            mLeftTopIcon.getIntrinsicHeight()+(int)mLeftTopPosY);

    mRightTopIcon = context.getResources().getDrawable(R.drawable.corners);
    mRightTopIcon.setBounds((int)mRightTopPosX, (int)mRightTopPosY,
            mRightTopIcon.getIntrinsicWidth()+(int)mRightTopPosX,
            mRightTopIcon.getIntrinsicHeight()+(int)mRightTopPosY);

    mLeftBottomIcon = context.getResources().getDrawable(R.drawable.corners);
    mLeftBottomIcon.setBounds((int)mLeftBottomPosX, (int)mLeftBottomPosY,
            mLeftBottomIcon.getIntrinsicWidth()+(int)mLeftBottomPosX,
            mLeftBottomIcon.getIntrinsicHeight()+(int)mLeftBottomPosY);

    mRightBottomIcon = context.getResources().getDrawable(R.drawable.corners);
    mRightBottomIcon.setBounds((int)mRightBottomPosX, (int)mRightBottomPosY,
            mRightBottomIcon.getIntrinsicWidth()+(int)mRightBottomPosX,
            mRightBottomIcon.getIntrinsicHeight()+(int)mRightBottomPosY);

尝试以下代码,拍照后,输入以下代码:

File f = new File(filePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

int angle = 0;

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}

Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

您是否已将此位放大器存储在文件中?Pratik,请参见插图,这将没有帮助,因为拍摄的图像区域是错误的