2017-07-28 7 views
0

Je souhaite créer une application qui lit une image, pixel par pixel, et utilise cette information pour créer une image clonée. Pour cela j'utilise 2 ImageViews, l'un a une image artistique et l'autre a une image vide (seulement un fond blanc).Clonage d'une image, pixel par pixel

Lorsque je clique sur le bouton pour démarrer le clonage d'image, je reçois ce message:

Malheureusement, l'application a arrêté

J'ai essayé plusieurs changements suggérés dans plusieurs postes, mais je ne pouvais pas régler ce problème.

import android.graphics.Bitmap; 
import android.graphics.Color; 
import android.graphics.drawable.BitmapDrawable; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 


public class MainActivity extends AppCompatActivity { 

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

     cloneAnImagePixelByPixel(); 
    } 

    public void cloneAnImagePixelByPixel() 
    { 

      final Button btn = (Button) findViewById(R.id.button); 

      btn.setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        ImageView artimg = (ImageView) findViewById(R.id.art); 
        ImageView emptyimg = (ImageView) findViewById(R.id.emptyim); 

        Bitmap bitmap1 = ((BitmapDrawable) artimg.getDrawable()).getBitmap(); 
        Bitmap bitmap2 = ((BitmapDrawable) emptyimg.getDrawable()).getBitmap(); 

        int img_height = bitmap1.getHeight(); 
        int img_width = bitmap1.getWidth(); 

        for (int i = 0; i < img_height; i++) 
        { 
         for (int k = 0; k < img_width; k++) 
         { 
          int pixel = bitmap1.getPixel(i, k); 

          int a = Color.alpha(pixel); 
          int r = Color.red(pixel); 
          int g = Color.green(pixel); 
          int b = Color.blue(pixel); 

          bitmap2.setPixel(i, k, Color.argb(a,r,g,b)); 

         } 
        } 
       } 
      }); 
    } 
} 

Et voici le code xml:

<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="com.example.mypc.creatingacloneimagefrompixels.MainActivity"> 

    <ImageView 
     android:id="@+id/art" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/sandart" 
     tools:layout_editor_absoluteX="125dp" 
     tools:layout_editor_absoluteY="57dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="42dp" /> 

    <ImageView 
     android:id="@+id/emptyim" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/empty" 
     tools:layout_editor_absoluteX="125dp" 
     tools:layout_editor_absoluteY="205dp" 
     android:layout_centerVertical="true" 
     android:layout_alignEnd="@+id/art" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CLONE IMAGE" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     tools:layout_editor_absoluteX="122dp" 
     tools:layout_editor_absoluteY="380dp" 
     android:layout_marginBottom="83dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignStart="@+id/emptyim" /> 
</RelativeLayout> 

Le journal des événements est ici:

28-Jul-17 
10:18 AM Executing tasks: [:app:assembleDebug] 

10:18 AM Gradle build finished in 11s 988ms 

10:38 AM Executing tasks: [:app:assembleDebug] 

10:38 AM Gradle build finished in 4s 956ms 

10:38 AM ADB rejected shell command (cat /proc/2560/stat): closed 

The gradle console is here: 

    Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies] 

Configuration on demand is an incubating feature. 
NDK is missing a "platforms" directory. 
If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to C:\Users\EASYPC\AppData\Local\Android\Sdk\ndk-bundle. 
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning. 

Incremental java compilation is an incubating feature. 
:app:preBuild UP-TO-DATE 
:app:preDebugBuild UP-TO-DATE 
:app:checkDebugManifest 
:app:preReleaseBuild UP-TO-DATE 
:app:prepareComAndroidSupportAnimatedVectorDrawable2600Alpha1Library 
:app:prepareComAndroidSupportAppcompatV72600Alpha1Library 
:app:prepareComAndroidSupportConstraintConstraintLayout102Library 
:app:prepareComAndroidSupportSupportCompat2600Alpha1Library 
:app:prepareComAndroidSupportSupportCoreUi2600Alpha1Library 
:app:prepareComAndroidSupportSupportCoreUtils2600Alpha1Library 
:app:prepareComAndroidSupportSupportFragment2600Alpha1Library 
:app:prepareComAndroidSupportSupportMediaCompat2600Alpha1Library 
:app:prepareComAndroidSupportSupportV42600Alpha1Library 
:app:prepareComAndroidSupportSupportVectorDrawable2600Alpha1Library 
:app:prepareDebugDependencies 
:app:compileDebugAidl UP-TO-DATE 
:app:compileDebugRenderscript UP-TO-DATE 
:app:generateDebugBuildConfig UP-TO-DATE 
:app:generateDebugResValues UP-TO-DATE 
:app:generateDebugResources UP-TO-DATE 
:app:mergeDebugResources UP-TO-DATE 
:app:processDebugManifest UP-TO-DATE 
:app:processDebugResources UP-TO-DATE 
:app:generateDebugSources UP-TO-DATE 
:app:preDebugAndroidTestBuild UP-TO-DATE 
:app:prepareComAndroidSupportTestEspressoEspressoCore222Library 
:app:prepareComAndroidSupportTestEspressoEspressoIdlingResource222Library 
:app:prepareComAndroidSupportTestExposedInstrumentationApiPublish05Library 
:app:prepareComAndroidSupportTestRules05Library 
:app:prepareComAndroidSupportTestRunner05Library 
:app:prepareDebugAndroidTestDependencies 
:app:compileDebugAndroidTestAidl UP-TO-DATE 
:app:processDebugAndroidTestManifest UP-TO-DATE 
:app:compileDebugAndroidTestRenderscript UP-TO-DATE 
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE 
:app:generateDebugAndroidTestResValues UP-TO-DATE 
:app:generateDebugAndroidTestResources UP-TO-DATE 
:app:mergeDebugAndroidTestResources UP-TO-DATE 
:app:processDebugAndroidTestResources UP-TO-DATE 
:app:generateDebugAndroidTestSources UP-TO-DATE 
:app:mockableAndroidJar UP-TO-DATE 
:app:preDebugUnitTestBuild UP-TO-DATE 
:app:prepareDebugUnitTestDependencies 

BUILD SUCCESSFUL 

Total time: 2.717 secs 
Executing tasks: [:app:assembleDebug] 

Configuration on demand is an incubating feature. 
NDK is missing a "platforms" directory. 
If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to C:\Users\EASYPC\AppData\Local\Android\Sdk\ndk-bundle. 
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning. 

Incremental java compilation is an incubating feature. 
:app:buildInfoDebugLoader 
:app:preBuild UP-TO-DATE 
:app:preDebugBuild UP-TO-DATE 
:app:checkDebugManifest 
:app:prepareComAndroidSupportAnimatedVectorDrawable2600Alpha1Library 
:app:prepareComAndroidSupportAppcompatV72600Alpha1Library 
:app:prepareComAndroidSupportConstraintConstraintLayout102Library 
:app:prepareComAndroidSupportSupportCompat2600Alpha1Library 
:app:prepareComAndroidSupportSupportCoreUi2600Alpha1Library 
:app:prepareComAndroidSupportSupportCoreUtils2600Alpha1Library 
:app:prepareComAndroidSupportSupportFragment2600Alpha1Library 
:app:prepareComAndroidSupportSupportMediaCompat2600Alpha1Library 
:app:prepareComAndroidSupportSupportV42600Alpha1Library 
:app:prepareComAndroidSupportSupportVectorDrawable2600Alpha1Library 
:app:prepareDebugDependencies 
:app:compileDebugAidl UP-TO-DATE 
:app:compileDebugRenderscript UP-TO-DATE 
:app:generateDebugBuildConfig UP-TO-DATE 
:app:generateDebugResValues UP-TO-DATE 
:app:generateDebugResources UP-TO-DATE 
:app:mergeDebugResources UP-TO-DATE 
:app:processDebugManifest 
:app:processDebugResources 
:app:generateDebugSources 
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE 
:app:javaPreCompileDebug 
:app:compileDebugJavaWithJavac UP-TO-DATE 
:app:mergeDebugShaders UP-TO-DATE 
:app:compileDebugShaders UP-TO-DATE 
:app:generateDebugAssets UP-TO-DATE 
:app:mergeDebugAssets UP-TO-DATE 
:app:processDebugJavaRes UP-TO-DATE 
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE 
:app:packageInstantRunResourcesDebug 
:app:fastDeployDebugExtractor UP-TO-DATE 
:app:generateDebugInstantRunAppInfo UP-TO-DATE 
:app:checkManifestChangesDebug 
:app:transformClassesWithExtractJarsForDebug UP-TO-DATE 
:app:transformClassesWithInstantRunVerifierForDebug UP-TO-DATE 
:app:transformClassesWithDependencyCheckerForDebug UP-TO-DATE 
:app:compileDebugNdk UP-TO-DATE 
:app:mergeDebugJniLibFolders UP-TO-DATE 
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE 
:app:transformNativeLibsAndResourcesWithJavaResourcesVerifierForDebug UP-TO-DATE 
:app:transformClassesWithInstantRunForDebug 
:app:transformClassesEnhancedWithInstantReloadDexForDebug 
:app:incrementalDebugTasks 
:app:preColdswapDebug 
:app:transformClassesWithInstantRunSlicerForDebug 
:app:transformClassesWithDexForDebug 
:app:validateSigningDebug 
:app:transformDexWithInstantRunDependenciesApkForDebug 
:app:transformDexWithInstantRunSlicesApkForDebug 
:app:packageDebug 
:app:buildInfoGeneratorDebug 
:app:compileDebugSources UP-TO-DATE 
:app:assembleDebug 

BUILD SUCCESSFUL 

Total time: 11.919 secs 
Executing tasks: [:app:assembleDebug] 

Configuration on demand is an incubating feature. 
NDK is missing a "platforms" directory. 
If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to C:\Users\EASYPC\AppData\Local\Android\Sdk\ndk-bundle. 
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning. 

Incremental java compilation is an incubating feature. 
:app:buildInfoDebugLoader 
:app:preBuild UP-TO-DATE 
:app:preDebugBuild UP-TO-DATE 
:app:checkDebugManifest 
:app:prepareComAndroidSupportAnimatedVectorDrawable2600Alpha1Library 
:app:prepareComAndroidSupportAppcompatV72600Alpha1Library 
:app:prepareComAndroidSupportConstraintConstraintLayout102Library 
:app:prepareComAndroidSupportSupportCompat2600Alpha1Library 
:app:prepareComAndroidSupportSupportCoreUi2600Alpha1Library 
:app:prepareComAndroidSupportSupportCoreUtils2600Alpha1Library 
:app:prepareComAndroidSupportSupportFragment2600Alpha1Library 
:app:prepareComAndroidSupportSupportMediaCompat2600Alpha1Library 
:app:prepareComAndroidSupportSupportV42600Alpha1Library 
:app:prepareComAndroidSupportSupportVectorDrawable2600Alpha1Library 
:app:prepareDebugDependencies 
:app:compileDebugAidl UP-TO-DATE 
:app:compileDebugRenderscript UP-TO-DATE 
:app:generateDebugBuildConfig UP-TO-DATE 
:app:generateDebugResValues UP-TO-DATE 
:app:generateDebugResources UP-TO-DATE 
:app:mergeDebugResources UP-TO-DATE 
:app:processDebugManifest UP-TO-DATE 
:app:processDebugResources UP-TO-DATE 
:app:generateDebugSources 
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE 
:app:javaPreCompileDebug 
:app:compileDebugJavaWithJavac UP-TO-DATE 
:app:mergeDebugShaders UP-TO-DATE 
:app:compileDebugShaders UP-TO-DATE 
:app:generateDebugAssets UP-TO-DATE 
:app:mergeDebugAssets UP-TO-DATE 
:app:processDebugJavaRes UP-TO-DATE 
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE 
:app:packageInstantRunResourcesDebug UP-TO-DATE 
:app:fastDeployDebugExtractor UP-TO-DATE 
:app:generateDebugInstantRunAppInfo UP-TO-DATE 
:app:checkManifestChangesDebug 
:app:transformClassesWithExtractJarsForDebug UP-TO-DATE 
:app:transformClassesWithInstantRunVerifierForDebug UP-TO-DATE 
:app:transformClassesWithDependencyCheckerForDebug UP-TO-DATE 
:app:compileDebugNdk UP-TO-DATE 
:app:mergeDebugJniLibFolders UP-TO-DATE 
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE 
:app:transformNativeLibsAndResourcesWithJavaResourcesVerifierForDebug UP-TO-DATE 
:app:transformClassesWithInstantRunForDebug UP-TO-DATE 
:app:transformClassesEnhancedWithInstantReloadDexForDebug UP-TO-DATE 
:app:incrementalDebugTasks UP-TO-DATE 
:app:preColdswapDebug 
:app:transformClassesWithInstantRunSlicerForDebug UP-TO-DATE 
:app:transformClassesWithDexForDebug UP-TO-DATE 
:app:validateSigningDebug 
:app:transformDexWithInstantRunDependenciesApkForDebug UP-TO-DATE 
:app:transformDexWithInstantRunSlicesApkForDebug UP-TO-DATE 
:app:packageDebug UP-TO-DATE 
:app:buildInfoGeneratorDebug 
:app:compileDebugSources UP-TO-DATE 
:app:assembleDebug 

BUILD SUCCESSFUL 

Total time: 4.89 secs 

Le Moniteur Android est ici:

07-28 10:38:46.196 2560-2560/? E/libprocessgroup: failed to make and chown /acct/uid_10080: Read-only file system 
07-28 10:38:46.196 2560-2560/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT? 
07-28 10:38:46.197 2560-2560/? I/art: Not late-enabling -Xcheck:jni (already on) 
07-28 10:38:46.211 2560-2570/? E/art: Failed sending reply to debugger: Broken pipe 
07-28 10:38:46.211 2560-2570/? I/art: Debugger is no longer active 
07-28 10:38:46.293 2560-2560/? I/InstantRun: starting instant run server: is main process 
07-28 10:38:46.334 2560-2560/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
07-28 10:38:46.426 2560-2583/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 

               [ 07-28 10:38:46.427 2560: 2560 D/   ] 
               HostConnection::get() New Host Connection established 0xb3ee71f0, tid 2560 


               [ 07-28 10:38:46.428 2560: 2560 W/   ] 
               Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 
07-28 10:38:46.428 2560-2560/? D/Atlas: Validating map... 
07-28 10:38:46.451 2560-2583/? I/OpenGLRenderer: Initialized EGL, version 1.4 
07-28 10:38:46.452 2560-2583/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
07-28 10:38:46.460 2560-2583/? D/EGL_emulation: eglCreateContext: 0xb3eb8940: maj 2 min 0 rcv 2 
07-28 10:38:46.464 2560-2583/? D/EGL_emulation: eglMakeCurrent: 0xb3eb8940: ver 2 0 
07-28 10:38:46.467 2560-2583/? D/OpenGLRenderer: Enabling debug mode 0 
07-28 10:38:46.500 2560-2583/? D/EGL_emulation: eglMakeCurrent: 0xb3eb8940: ver 2 0 
07-28 10:38:46.555 2560-2560/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
07-28 10:38:51.028 2560-2560/com.example.easypc.creatingacloneimagefrompixels D/AndroidRuntime: Shutting down VM 
07-28 10:38:51.030 2560-2560/com.example.easypc.creatingacloneimagefrompixels E/AndroidRuntime: FATAL EXCEPTION: main 
                           Process: com.example.easypc.creatingacloneimagefrompixels, PID: 2560 
                           java.lang.IllegalStateException 
                            at android.graphics.Bitmap.setPixel(Bitmap.java:1420) 
                            at com.example.easypc.creatingacloneimagefrompixels.MainActivity$1.onClick(MainActivity.java:53) 
                            at android.view.View.performClick(View.java:4780) 
                            at android.view.View$PerformClick.run(View.java:19866) 
                            at android.os.Handler.handleCallback(Handler.java:739) 
                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                            at android.os.Looper.loop(Looper.java:135) 
                            at android.app.ActivityThread.main(ActivityThread.java:5254) 
                            at java.lang.reflect.Method.invoke(Native Method) 
                            at java.lang.reflect.Method.invoke(Method.java:372) 
                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+3

Logcat, s'il vous plaît. https://stackoverflow.com/questions/23353173/unheureusement-myapp-has-stopped-how-can-i-solve-this –

+0

Il est un peu difficile d'identifier le problème sans votre StackTrace. Veuillez mettre à jour votre question –

+0

Salut Vinit Merci pour votre commentaire. J'ai déjà ajouté la console Gradle, le journal des événements et le contenu du moniteur Android. –

Répondre

0

Je crois que votre problème est que bitmap2 n'est pas mutable. Vous pouvez créer un bitmap mutable en utilisant Bitmap.copy() avec isMutable défini sur true ou en utilisant Bitmap.create(width,height,config). Vous pouvez également charger un bitmap modifiable en définissant inMutable sur true dans BitmapFactory.Options en utilisant BitmapFactory

+0

Bonjour @cwbowron J'ai fait le changement dans le code (voir ci-dessous), mais l'application ne fonctionne toujours pas. je ne suis pas sûr si le chemin de l'image dans BitmapFactory.decodeFile ("/ images/empty.png", options); est correct. Le chemin par rapport au nom du projet est le suivant: F: \ Android-Projets \ CreatingACloneImageFromPixels \ app \ images \ empty.png. Je suppose que le dossier "app" est la racine de l'application. –

+0

(suite) Les changements de code sont entre la ligne deux, plus la ligne commentée ci-dessus:. 'recueil HSC // Bitmap bitmap2 = ((BitmapDrawable) emptyimg.getDrawable()) GetBitmap(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Options de BitmapFactory.Options = new BitmapFactory.Options () options.inMutable = true; Bitmap outputBitmap = BitmapFactory.decodeFile ("/ images/empty.png", options); emptyimg.setImageBitmap (outputBitmap); // ~~~~~~~~~~~~~~~~~~~~~~~~ 'code' –

+0

(suite) Ai-je fait les déclarations correctes et utilisé les bons paramètres ? –