Beyka / Android-TiffBitmapFactory

MIT License
131 stars 46 forks source link

.tif convert to .png failed !!!(needed for my master thesis) #56

Closed MomoSolaris8 closed 1 year ago

MomoSolaris8 commented 1 year ago

Description: I got a task to load an image in .tif file format in android studio (really phone)and then do a method of image transformation (python code to handle image transformation, like gabor filter/transformation).

My steps:

  1. The android studio phone first has to load the 1.tif image from the real phone , and convert 1.tif to 1.png.
  2. create on imageview to display 1.png in the app.

Problem: 1.I chose to finish selecting the image from the gallery and the app shutdwon!!!

  1. I readed your READEME.md, i have NDK in my Android Studio installed , but what is the tiffbitmapfactory folder means ?? I have had install the implementation 'io.github.beyka:Android-TiffBitmapFactory:0.9.9.0' in my gradle , do I have problem, if I don't have this tiffbitmapfactory folder??

and I have this logcat error, I don`t really understand this meaning(search Beyka in logcat search) :

2022-11-18 13:02:52.625 32130-32130 DEBUG pid-32130 A #02 pc 000000000001b818 /data/app/~~njWGMcyI4gUAcaf-MlxABg==/com.example.demo8-fENlZr-_q_0NKYWEBNe8Jg==/lib/arm64/libtiffconverter.so (Java_org_beyka_tiffbitmapfactory_TiffConverter_convertTiffPng+112) (BuildId: df448798ff7fc0449147e9e7908310cbf5fdb9fe) ---------------------------- PROCESS ENDED (31712) for package com.example.demo8 ---------------------------- ---------------------------- PROCESS STARTED (32188) for package com.example.demo8 ----------------------------

Build from sources

To build native part of library use [Android-NDK-bundle](https://developer.android.com/tools/sdk/ndk/index.html).

To start build go to tiffbitmapfactory folder and run

ndk-build NDK_PROJECT_PATH=src/main

`import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView;

import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.FileUtils; import android.os.ParcelFileDescriptor; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import com.chaquo.python.PyObject; import com.chaquo.python.Python; import com.chaquo.python.android.AndroidPlatform;

import org.beyka.tiffbitmapfactory.CompressionScheme; import org.beyka.tiffbitmapfactory.IProgressListener; import org.beyka.tiffbitmapfactory.TiffBitmapFactory; import org.beyka.tiffbitmapfactory.TiffConverter;

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

TextView textView8;
TextView Viewpath;
//int SELECT_IMAGE_CODE =1;
ImageView imageView;
Button btn ;

//private static final int Read_Permission= 101;

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

     Viewpath = findViewById(R.id.path);

     btn = findViewById(R.id.gallery);
     //imageView = findViewById(R.id.imageView);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent,3);

        }
    });
    // python script code
    textView8 = (TextView) findViewById(R.id.textView8);

    if(!Python.isStarted()){
        Python.start(new AndroidPlatform(this));
    }

    Python py = Python.getInstance();
    PyObject pyobj = py.getModule("hello");
    // give python script name
    // now call this function
    PyObject obj = pyobj.callAttr("main");
    // now set return text to textview
    textView8.setText(obj.toString());

}
IProgressListener progressListener = new IProgressListener() {
    @Override
    public void reportProgress(long processedPixels, long totalPixels) {
        Log.v("Progress reporter", String.format("Processed %d pixels from %d", processedPixels, totalPixels));
    }
};
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    Uri selectedImage = data.getData();
  //  Context context = MainActivity.this;
    // test the Image path
    //Context context = getApplicationContext();
    //String path = RealPathUtil.getRealPath(context,selectedImage);

   //Viewpath.setText("Uri="+ selectedImage.toString()+"\n\nPath="+path);

    try {

       // imageView.setImageURI(selectedImage);

        TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions();
        options.throwExceptions = false;
        options.availableMemory = 128 * 1024 * 1024;
        options.readTiffDirectory = 1;
         // .tif convert to png
        TiffConverter.convertTiffPng("/storage/emulated/0/Download/Response_1.tif", "/storage/emulated/0/Download/1.png", options, progressListener);
        //ImageView imageView = findViewById(R.id.imageView);
        imageView = findViewById(R.id.imageView);
        // setImage from local path
        imageView.setImageURI(Uri.fromFile(new File("/storage/emulated/0/Download/1.png")));
    }catch (Exception e) {
        e.printStackTrace();
    }

}

    }

.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="341dp"
    android:layout_height="396dp"
    android:layout_above="@+id/textView8"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_marginStart="50dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="35dp" />

<Button
    android:id="@+id/gallery"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Load Image Form Gallery"
    android:textAllCaps="false"
    android:textSize="21sp" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="165dp"
    android:layout_height="56dp"
    android:layout_above="@+id/gallery"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_marginStart="113dp"
    android:layout_marginTop="33dp"
    android:layout_marginEnd="133dp"
    android:layout_marginBottom="18dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.467"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/gallery"
    app:layout_constraintVertical_bias="0.2"
    tools:ignore="MissingConstraints" />

`

Beyka commented 1 year ago

options.readTiffDirectory = 1; Are you sure you have more than one page in your tiff file? If no - just not use this option

Also there is new version 0.9.9.1. plz try it

MomoSolaris8 commented 1 year ago

And firstly, I really thank you that you created the beautiful, useful project !

Question 1: options.readTiffDirectory = 1; Are you sure you have more than one page in your tiff file?

what means page? I really do not know it ? Which option should I use ? Please help me !

Question 2: Yes(in my view ) , I have 10 example .tif Images under storage/emulated/0/Download/ , and I picked only one image, such as Storage/Simulation/0/Download/Response_1.tif, to convert to png.

Question 3: In your README.md

//Convert to PNG
TiffConverter.convertTiffPng("/sdcard/in.tif", "/sdcard/out.png", options, progressListener);

However I cannot find the sdcard in my phone , so i print the uri path using https://gist.github.com/tatocaster/32aad15f6e0c50311626 to get the imagepath. My Image path : Storage/Simulation/0/Download/Response_1.tif Myphone is Samsung SM-A528B

I have previously installed only old version 0.9.9.0 , now installed new version 0.9.9.1. And still I get shutdown!!!

Beyka commented 1 year ago

what means page? Tiff files could have many pages. Each page contains some image. Numeration of pages starts from 0. if You not set readTiffDirectory, then page №0 will be used. SO you can just delete this option from your code. What android version do you use? if 10 or greater you shouldn't use file paths, you should use file descriptors. if 9 and below - did you get permission to write to storage?