2017-07-28 65 views
0

克隆圖像,像素我想創建一個應用程序,通過像素讀取圖像,像素,並使用這些信息來建立一個克隆的圖像。爲此,我使用了2個ImageViews,其中一個具有藝術圖片,另一個具有空白圖片(僅白色背景)。由像素

當我點擊按鈕啓動圖像克隆,我得到這個消息:

不幸的是,應用程序已經停止

我試過幾個職位提出了若干修改,但我做不到理清這個問題。

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

         } 
        } 
       } 
      }); 
    } 
} 

這裏是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> 

事件日誌是在這裏:

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 

Android的顯示器是在這裏:

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請設置inMutable爲true BitmapFactory.Options加載一個可變的位圖。 https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this –

+0

這是一個有點難以確定問題,而無需您的堆棧跟蹤。請更新您的問題。 –

+0

嗨Vinit 謝謝你的評論。 我已經添加了Gradle控制檯,事件日誌和Android監視器內容。 –

回答

0

我相信你的問題是bitmap2不是可變的。您可以創建使用Bitmap.copy()一個可變的位圖isMutable設置爲true或使用Bitmap.create(width,height,config)。您也可以通過使用BitmapFactory

+0

喜@cwbowron 我沒有在代碼中的變化(見下文),但應用程序仍然無法正常工作。我不知道如果在BitmapFactory.decodeFile(「/ images/empty.png」,選項)圖像的路徑;是正確的。與項目名稱相關的路徑如下:F:\ Android-Projects \ CreatingACloneImageFromPixels \ app \ images \ empty.png。我假設「應用程序」文件夾是應用程序的根。 –

+0

(續) 代碼更改在兩行加上面註釋的行之間: 'code' //位圖bitmap2 =((BitmapDrawable)emptyimg.getDrawable())。getBitmap(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BitmapFactory.Options選項=新BitmapFactory.Options( ); options.inMutable = true; 位圖outputBitmap = BitmapFactory.decodeFile(「/ images/empty.png」,options); emptyimg.setImageBitmap(outputBitmap); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'code' –

+0

(continuation) 我做了正確的聲明並使用了正確的參數? –