Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crashed while setting bitmap #43

Open
uzzam-web-dev opened this issue Nov 29, 2022 · 2 comments
Open

App crashed while setting bitmap #43

uzzam-web-dev opened this issue Nov 29, 2022 · 2 comments

Comments

@uzzam-web-dev
Copy link

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

@KamranKhanDemo
Copy link

your ImagePath is null, fix that it should work then

@KamranKhanDemo
Copy link

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;
    }
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants