【问题标题】:Hello i have a issue with this example你好我对这个例子有疑问
【发布时间】:2016-06-15 07:35:14
【问题描述】:

我正在发布示例,它会制作一张照片并让您看到预览,但我有一个问题:问题是,如果您尝试使用此代码,您将看到预览,但它的方向不正确,请在那里谁能解决这个问题?

public class MainActivity extends ActionBarActivity {

private ImageSurfaceView mImageSurfaceView;
private Camera camera;

private FrameLayout cameraPreviewLayout;
private ImageView capturedImageHolder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    cameraPreviewLayout = (FrameLayout)findViewById(R.id.camera_preview);
    capturedImageHolder = (ImageView)findViewById(R.id.captured_image);

    camera = checkDeviceCamera();
    mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
    cameraPreviewLayout.addView(mImageSurfaceView);

    Button captureButton = (Button)findViewById(R.id.button);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            camera.takePicture(null, null, pictureCallback);
        }
    });
}
private Camera checkDeviceCamera(){
    Camera mCamera = null;
    try {
        mCamera = Camera.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mCamera;
}

PictureCallback pictureCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        if(bitmap==null){
            Toast.makeText(MainActivity.this, "Captured image is empty", Toast.LENGTH_LONG).show();
            return;
        }
        capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200 ));
    }
};

private Bitmap scaleDownBitmapImage(Bitmap bitmap, int newWidth, int newHeight){
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
    return resizedBitmap;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

ImageSurfaceView 类

public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private Camera camera;
private SurfaceHolder surfaceHolder;

public ImageSurfaceView(Context context, Camera camera) {
    super(context);
    this.camera = camera;
    this.surfaceHolder = getHolder();
    this.surfaceHolder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        this.camera.setPreviewDisplay(holder);
        this.camera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    this.camera.stopPreview();
    this.camera.release();
}
}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:baselineAligned="false">

<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="match_parent"
    android:layout_height="300dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capture_button"
    android:id="@+id/button"
    android:layout_below="@+id/camera_preview"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/captured_image"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="15dp"
    android:contentDescription="@string/desc" />

问题是,如果您尝试此代码,您会看到预览,但它的方向不正确,请问有人可以解决这个问题吗?

【问题讨论】:

  • you'll see the preview but it is not oriented correctly 用图片更新您的问题或详细说明问题所在。另外,请阅读How do I ask a good question?
  • 这个标题太笼统了,试着改一下来说明实际问题(比如“相机预览的方向不正确”)。

标签: java android xml


【解决方案1】:

您必须使用 Exifinterface 和 Matrix 才能获得正确的方向。 试试这个:

 public static void handleImageRotation(Context context, File mFileTemp) {

    if (!mFileTemp.exists()) {
        Toast.makeText(context, "File not found", Toast.LENGTH_SHORT).show();
        return;
    }


    ExifInterface exif = null;
    int orientation = ExifInterface.ORIENTATION_NORMAL;
    try {
        exif = new ExifInterface(mFileTemp.getAbsolutePath());

        orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Bitmap temp = BitmapFactory.decodeFile(mFileTemp.getAbsolutePath());

    if (temp == null) Log.e(TAG, "Bitmap from File is null");

    Matrix matrix = new Matrix();

    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        matrix.postRotate(90);
        temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true);
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        matrix.postRotate(180);
        temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true);
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        matrix.postRotate(270);
        temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true);
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    temp.compress(Bitmap.CompressFormat.JPEG, 40, bos);

    byte[] bitmapdata = bos.toByteArray();

    // write the bytes in file
    try {
        FileOutputStream fos = new FileOutputStream(mFileTemp);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

【讨论】:

    【解决方案2】:

    这是有问题的代码:

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
    
            Camera.Parameters parameters = camera.getParameters();
    
            parameters.setRotation(270); //set rotation to save the picture
    
            camera.setDisplayOrientation(90);
    
            //set the rotation for preview camera
    
            camera.setParameters(parameters);
            this.camera.setPreviewDisplay(holder);
            this.camera.startPreview();
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-09
      • 1970-01-01
      • 2020-08-13
      • 2019-06-24
      • 2021-03-02
      • 2020-12-30
      • 2017-02-22
      • 2019-05-06
      • 1970-01-01
      相关资源
      最近更新 更多