1

アプリの画像関連にImageMagickライブラリを使いたいのですが、サンプルコードを作ったのですが、実行するとスピナーしか表示されず、処理後の画像が表示されません。これは私が使用しているコードです:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        // InputStream stream = getResources().openRawResource(R.raw.app_notes);


        try {
            final MagickImage img = new MagickImage(new ImageInfo("/sdcard/newsample.jpeg"));

//          setContentView(R.layout.main);
            Spinner s = (Spinner) findViewById(R.id.spinner);

            ArrayAdapter adapter = ArrayAdapter.createFromResource(MainActivity.this,R.array.effects, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);

            s.setOnItemSelectedListener(new OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view,
                        int pos, long id) {
                    ProgressThread t = new ProgressThread(img, pos);
                    //runOnUiThread(t);
                    t.run();
                }

                public void onNothingSelected(AdapterView<?> parent) {
                    // Do nothing.
                }
            });
            ImageView iv = (ImageView) findViewById(R.id.imageView);
            iv.setImageBitmap(MagickBitmap.ToBitmap(img));
        } catch (MagickApiException e) {
            e.printStackTrace();
        } catch (MagickException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*
         * TextView tv = new TextView(this);
         * tv.setText(DisplayImageMetaData.displayMagickImage(img));
         * setContentView(tv);
         */
        int count = 2;
        MagickImage images[] = new MagickImage[count];
        for (int i = 0; i < count; i++) {
            String path = "/mnt/sdcard/" + String.valueOf(i + 1) + ".jpeg";
            try {
                images[i] = new MagickImage(new ImageInfo(path));
            } catch (MagickException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            MagickImage image = new MagickImage(images);
            image.setImageFormat("gif");
            String fn = Environment.getExternalStorageDirectory() + "/Pictures/test.gif";
            image.setFileName(fn);
            ImageInfo info = new ImageInfo(fn);
            info.setMagick("gif");
            //image.writeImage(info);
            byte blob[] = image.imageToBlob(info);
            FileOutputStream fos = new FileOutputStream(fn);
            fos.write(blob);
            fos.close();
            FileOutputStream testOS = new FileOutputStream("/mnt/sdcard/test.txt");
            testOS.write("abc".getBytes());
            testOS.close();
        } catch (MagickException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    protected Dialog onCreateDialog(int id, Bundle bundle) {
        switch (id) {
        case PROGRESS_DIALOG:
            /*
             * ProgressDialog dialog =
             * ProgressDialog.show(AndroidMagickActivity.this, "",
             * "Loading. Please wait...", true);
             */
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMessage("Loading...");
            return progressDialog;
            // return dialog;
        default:
            return null;
        }
    }


    /** Nested class that performs progress calculations (counting) */
    private class ProgressThread extends Thread {

        MagickImage img;
        int pos;

        ProgressThread(MagickImage image, int pos) {
            this.img = image;
            this.pos = pos;
        }

        public void run() {
            int effect = 0;
            MainActivity.this.showDialog(PROGRESS_DIALOG);
            switch (pos) {
            case 1:
                effect = NoiseType.UndefinedNoise;
                break;
            case 2:
                effect = NoiseType.UniformNoise;
                break;
            case 3:
                effect = NoiseType.GaussianNoise;
                break;
            case 4:
                effect = NoiseType.MultiplicativeGaussianNoise;
                break;
            case 5:
                effect = NoiseType.ImpulseNoise;
                break;
            case 6:
                effect = NoiseType.LaplacianNoise;
                break;
            case 7:
                effect = NoiseType.PoissonNoise;
                break;
            }
            Bitmap bitmap = null;
            try {
                ImageView iv = (ImageView) findViewById(R.id.imageView);

                MagickImage image = null;
                if (pos < 8)
                    image = img.addNoiseImage(effect);
                else if (pos == 9)
                    image = img.blurImage(5, 1);
                else if (pos == 10)
                    image = img.charcoalImage(5, 1);
                else if (pos == 11)
                    image = img.edgeImage(0);
                if (image != null) {

                    bitmap = MagickBitmap.ToBitmap(image);
                    iv.setImageBitmap(bitmap);
                }

                MainActivity.this.dismissDialog(PROGRESS_DIALOG);
            } catch (MagickException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }    

これはxmlファイルです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

</LinearLayout>

これはログ ファイルです。

09-25 22:45:13.322: V/Magick(554): Attempting to read from file /mnt/sdcard/1.jpeg
09-25 22:45:13.741: V/Magick(554): ReadImage completed
09-25 22:45:13.741: V/Magick(554): Attempting to read from file /mnt/sdcard/2.jpeg
09-25 22:45:14.112: V/Magick(554): ReadImage completed
09-25 22:45:14.534: D/dalvikvm(164): GC_EXPLICIT freed 151K, 7% free 21612K/23111K, paused 530ms+341ms
09-25 22:45:17.378: W/ActivityManager(80): Launch timeout has expired, giving up wake lock!
09-25 22:45:18.417: W/ActivityManager(80): Activity idle timeout for ActivityRecord{4117ad80 com.example.libmagick/.MainActivity}
09-25 22:45:23.281: I/ActivityManager(80): Process com.android.mms (pid 257) has died.
09-25 22:45:23.312: D/CountryDetectorService(80): No listener is left
09-25 22:45:30.272: D/dalvikvm(164): GC_EXPLICIT freed 57K, 7% free 21556K/23111K, paused 1035ms+723ms
09-25 22:45:34.442: D/dalvikvm(195): GC_EXPLICIT freed 112K, 5% free 12588K/13191K, paused 1724ms+88ms
09-25 22:45:41.332: D/dalvikvm(314): GC_EXPLICIT freed 158K, 5% free 12155K/12675K, paused 1287ms+291ms
09-25 22:45:59.741: V/TLINE(554): new: android.text.TextLine@40c43e30
09-25 22:46:00.462: D/dalvikvm(554): GC_FOR_ALLOC freed 3165K, 22% free 14154K/17927K, paused 158ms
09-25 22:46:00.462: I/dalvikvm-heap(554): Grow heap (frag case) to 15.274MB for 1155904-byte allocation
09-25 22:46:00.662: D/dalvikvm(554): GC_CONCURRENT freed <1K, 15% free 15283K/17927K, paused 11ms+4ms
09-25 22:46:00.922: D/dalvikvm(554): GC_FOR_ALLOC freed 1K, 15% free 15282K/17927K, paused 71ms
09-25 22:46:00.952: I/dalvikvm-heap(554): Grow heap (frag case) to 17.232MB for 2054928-byte allocation
09-25 22:46:01.062: D/dalvikvm(554): GC_FOR_ALLOC freed 0K, 14% free 17288K/19975K, paused 95ms
09-25 22:46:01.262: D/dalvikvm(554): GC_CONCURRENT freed <1K, 14% free 17289K/19975K, paused 9ms+4ms
09-25 22:46:02.402: D/dalvikvm(164): GC_EXPLICIT freed 5K, 7% free 21551K/23111K, paused 5ms+129ms
09-25 22:46:06.522: D/dalvikvm(554): GC_FOR_ALLOC freed 1286K, 18% free 16453K/19975K, paused 73ms
09-25 22:46:06.542: I/dalvikvm-heap(554): Grow heap (frag case) to 17.881MB for 1536016-byte allocation
09-25 22:46:06.722: D/dalvikvm(554): GC_FOR_ALLOC freed <1K, 17% free 17953K/21511K, paused 91ms
09-25 22:46:06.922: D/dalvikvm(554): GC_CONCURRENT freed <1K, 17% free 17953K/21511K, paused 7ms+4ms
09-25 22:46:07.212: D/dalvikvm(554): GC_FOR_ALLOC freed <1K, 17% free 17952K/21511K, paused 72ms
09-25 22:46:07.232: I/dalvikvm-heap(554): Grow heap (frag case) to 19.345MB for 1536016-byte allocation
09-25 22:46:07.422: D/dalvikvm(554): GC_FOR_ALLOC freed 0K, 16% free 19452K/23047K, paused 107ms
09-25 22:46:07.531: D/dalvikvm(554): GC_FOR_ALLOC freed 0K, 16% free 19452K/23047K, paused 97ms
09-25 22:46:07.552: I/dalvikvm-heap(554): Grow heap (frag case) to 20.810MB for 1536016-byte allocation
09-25 22:46:07.722: D/dalvikvm(554): GC_FOR_ALLOC freed 0K, 15% free 20952K/24583K, paused 138ms
09-25 22:46:08.781: I/ActivityManager(80): Displayed com.example.libmagick/.MainActivity: +1m1s413ms
09-25 22:46:08.781: W/InputManagerService(80): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@411f68b8
4

0 に答える 0