mayuce / AndroidDocumentScanner

This library helps to scan a document like CamScanner.
https://www.linkedin.com/in/mayuce/
MIT License
533 stars 168 forks source link

App crashed while setting bitmap #43

Open uzzam-web-dev opened 1 year ago

uzzam-web-dev commented 1 year ago

I am getting the bitmap correctly but when I setImage to documentScanner the app crashed. And the error is NullPointerException

KamranKhanDemo commented 10 months ago

your ImagePath is null, fix that it should work then

KamranKhanDemo commented 10 months ago

The follwoing code I have written in Java for ImageCropActivity, you can use that if you are using Java, or better to use Already written Kotlin Code `import androidx.appcompat.app.AppCompatActivity;

import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar;

import com.your_project.R;

import com.labters.documentscanner.DocumentScannerView;

import kotlin.Unit;

public class ImageCropActivity extends AppCompatActivity {

private static final String FILE_DIR = "FileDir";
DocumentScannerView documentScannerView;

ImageView resultImage;
ProgressBar progressBar;
Button btnImageCrop;

private static ImageCroppedCallBack callback;

public static Intent newIntent(Context context, String selectedFilePath, ImageCroppedCallBack imageCroppedCallBack) {
    Intent intent = new Intent(context, ImageCropActivity.class);
    intent.putExtra(FILE_DIR, selectedFilePath);
    //intent.putExtra("callback",callback);
    callback = imageCroppedCallBack;

    return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_crop);
    documentScannerView = findViewById(R.id.document_scanner);
    btnImageCrop = findViewById(R.id.btnImageCrop);
    progressBar = findViewById(R.id.progressBar);
    resultImage = findViewById(R.id.result_image);

    String filePath = getIntent().getStringExtra(FILE_DIR);
    Bitmap bitmap = assetToBitmap(filePath);

    //ImageProcessingUtils.processImage(filePath, filePath);

    LoadListenerFunction onLoadListener = new LoadListenerFunction() {
        @Override
        public Unit invoke(Boolean loading) {
            // Your implementation here
            if (loading) {
                // Handle loading state
            } else {
                // Handle not loading state
            }
            progressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
            return Unit.INSTANCE;
        }
    };

    documentScannerView.setOnLoadListener(onLoadListener);

    documentScannerView.setImage(bitmap);

    btnImageCrop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);
            // Simulate coroutine behavior using a separate thread or AsyncTask
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Bitmap image = documentScannerView.getCroppedImage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            resultImage.setVisibility(View.VISIBLE);
                            resultImage.setImageBitmap(image);
                            callback.onImageCropped(image);
                            finish();
                        }
                    });
                }
            }).start();
        }
    });
}

private Bitmap assetToBitmap(String file) {
    try {

        return BitmapFactory.decodeFile(file);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

} `