如何在Java中获取Fotoapparat预览? (Android)

时间:2019-06-21 02:55:54

标签: java android fotoapparat

我正在尝试在Android应用中设置Fotoapparat库。我想我正在按照每个步骤进行操作,但是一定有问题,因为我看不到相机的任何预览,闪光灯无法打开,并且在debug中看到的照片是空的。

问题出在哪里?我没主意了。

现在我有以下代码:

MainActivity:

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.File;

import io.fotoapparat.Fotoapparat;
import io.fotoapparat.log.LoggersKt;
import io.fotoapparat.parameter.ScaleType;
import io.fotoapparat.result.BitmapPhoto;
import io.fotoapparat.selector.FlashSelectorsKt;
import io.fotoapparat.selector.FocusModeSelectorsKt;
import io.fotoapparat.selector.LensPositionSelectorsKt;
import io.fotoapparat.selector.ResolutionSelectorsKt;
import io.fotoapparat.selector.SelectorsKt;
import io.fotoapparat.view.CameraView;

public class MainActivity extends AppCompatActivity {
    private CameraView mCameraView;
    private Button mTakePhotoButton;
    private Fotoapparat mFotoapparat;

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

        mCameraView = findViewById(R.id.camera_view);
        mTakePhotoButton = findViewById(R.id.take_photo_button);


        mFotoapparat = Fotoapparat
                .with(getApplicationContext())
                .into(mCameraView)
                .previewScaleType(ScaleType.CenterCrop)
                .photoResolution(ResolutionSelectorsKt.highestResolution())
                .lensPosition(LensPositionSelectorsKt.back())
                .focusMode(SelectorsKt.firstAvailable(
                        FocusModeSelectorsKt.fixed()
                ))
                .flash(SelectorsKt.firstAvailable(
                        FlashSelectorsKt.torch()
                ))
                .logger(LoggersKt.loggers(
                        LoggersKt.logcat(),
                        LoggersKt.fileLogger(this)
                ))
                .build();

            mTakePhotoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
                System.out.println(dir.getName());

                BitmapPhoto photo = null;

                try{
                    photo = mFotoapparat.takePicture().toBitmap().await();
                } catch (Exception e){}

                //mFotoapparat.takePicture().saveToFile(dir);
            }
        });
    }

    public void TakePicture(View view){
        mFotoapparat.takePicture();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
        mFotoapparat.start();
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    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"
    tools:context=".MainActivity">

    <io.fotoapparat.view.CameraView
        android:id="@+id/camera_view"
        android:layout_width="match_parent"
        android:layout_height="522dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/take_photo_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="TakePicture"
        android:text="Take Picture"
        app:layout_constraintBottom_toBottomOf="@+id/camera_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/camera_view"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

</android.support.constraint.ConstraintLayout>

我的Gradle中还包含了implementation 'io.fotoapparat:fotoapparat:2.7.0'以及相机和外部存储的权限。

0 个答案:

没有答案