如何确定CamePreview的大小?

时间:2019-02-11 17:00:32

标签: java android android-camera

我的显示应用具有以下分辨率:我的设备的宽1080高1920。应用始终处于纵向模式。我的相机分辨率为1920宽,高度为1080。 预览显示正确。但是我需要从camerapreview的不同空间获取像素(x,y)。我的x> 1080是因为我的应用程序分辨率和相机分辨率不同。 也许我为预览创建了错误的类,请帮忙。

ShowCamera.java

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

Camera camera;
SurfaceHolder holder;
MainActivity mainActivity;

public static String TAG = "ST_V: ";

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


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Log.d(TAG, "SurfaceChanged APPLY");
    try {
        camera.stopPreview();
        camera.setPreviewDisplay(holder);
        camera.setPreviewCallback(this); // this
        camera.startPreview();
    } catch (Exception ex) {
        Log.d(TAG,"error surface changed");
    }
}

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

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "SurfaceCreated OK");
    Camera.Parameters params = camera.getParameters();
    // add
    Camera.Size cameraSize = camera.getParameters().getPictureSize();
    mBitmapChanged = Bitmap.createBitmap(cameraSize.width, cameraSize.height, Bitmap.Config.ARGB_8888);

    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size mSize = null;

    for (Camera.Size size : sizes) {
        mSize = size;
    }
    //change orientation
    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        params.set("orientation", "portrait");
        camera.setDisplayOrientation(90);
        params.setRotation(90);
    } else {
        params.set("orientation", "landscape");
        camera.setDisplayOrientation(0);
        params.setRotation(0);
    }

    params.setPictureSize(mSize.width, mSize.height);

    camera.setParameters(params);
    try {
        camera.setPreviewCallback(this);
        camera.setPreviewDisplay(holder);
        camera.startPreview();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
Button btn_run;
Button btn_select;
Button btn_tracking;
TextView tv;
TextView xS;
FrameLayout camera_preview;
Camera camera;
ShowCamera showCamera;
DrawView drawView;

public static String TAG = "ST_V: ";

@Override
protected void onCreate(Bundle savedInstanceState) {
    OpenCVLoader.initDebug();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // elements from activity main
    btn_run = findViewById(R.id.btn_run);
    btn_select = findViewById(R.id.btn_selectStar);
    btn_tracking = findViewById(R.id.btn_tracking);
    camera_preview = findViewById(R.id.camera_preview);
    drawView = (DrawView) findViewById(R.id.drawViewID);
    tv = findViewById(R.id.tv);
    xS = findViewById(R.id.xS);

    // open camera
    camera = Camera.open(0);
    showCamera = new ShowCamera(this, camera);
    camera_preview.addView(showCamera);
    camera.setDisplayOrientation(90);
    camera_preview.setVisibility(View.VISIBLE);
    addTouchListener();

    Log.d(TAG, String.valueOf(camera_preview.getHeight()) + String.valueOf(camera_preview.getWidth()));

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_tracking"
        android:layout_width="120dp"
        android:layout_height="42dp"
        android:text="Отследить"
        android:textAllCaps="false"
        android:textSize="12sp"
        android:layout_gravity="left"
        android:layout_marginTop="500dp"
        android:layout_marginLeft="120dp"
        android:onClick="btnTrackingOnClick"
        />

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

    </FrameLayout>

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <Button
        android:id="@+id/btn_selectStar"
        android:layout_width="120dp"
        android:layout_height="42dp"
        android:onClick="btnSelectStarOnClick"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/btn_select"
        android:textAllCaps="false"
        android:textSize="12sp"
        android:layout_gravity="left"
        android:layout_marginTop="500dp"/>

    <TextView
        android:id="@+id/xS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="100dp"
        android:text="TextView" />

    <Button
        android:id="@+id/btn_run"
        android:layout_width="120dp"
        android:layout_height="42dp"
        android:layout_gravity="right"
        android:layout_marginTop="500dp"
        android:onClick="btnRunOnClick"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/btn_start"
        android:textAllCaps="false"
        android:textSize="12sp" />

    <Button
        android:id="@+id/btn_scan"
        android:layout_width="120dp"
        android:layout_height="42dp"
        android:layout_gravity="left"
        android:onClick="btnScanOnClick"
        android:layout_marginTop="540dp"
        android:paddingLeft="10dp"
        android:text="Button"
        android:textAllCaps="false"
        android:textSize="12sp" />

    <com.example.vitalja.stars.DrawView
        android:id="@+id/drawViewID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="visible" />



</FrameLayout>

0 个答案:

没有答案