Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package by.name.photoeditor;
  2.  
  3. import android.content.DialogInterface;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.net.Uri;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.SurfaceView;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.ImageView;
  14. import android.widget.TabHost;
  15. import android.widget.Toast;
  16. import android.provider.MediaStore;
  17. import android.view.View.OnClickListener;
  18.  
  19.  
  20. import java.io.IOException;
  21.  
  22.  
  23. public class EditorActivity extends AppCompatActivity implements OnClickListener {
  24.  
  25.     ImageView editorView;
  26.  
  27.     String currentFilter;
  28.  
  29.  
  30.     /*
  31.     if(!(((BitmapDrawable)editorView.getDrawable()).getBitmap().isRecycled())){
  32.                         ((BitmapDrawable)editorView.getDrawable()).getBitmap().recycle();
  33.                         processedBitmap = BitmapProcessing.sepia(photoBitmap);
  34.                     }
  35.  
  36.     */
  37.  
  38.     @Override
  39.     public void onClick(View v) {
  40.  
  41.  
  42.         switch (v.getId()){
  43.             case R.id.btnWithoutfilters:
  44.                 if(currentFilter != "nofilter"){
  45.  
  46.                     processedBitmap = photoBitmap;
  47.  
  48.  
  49.                     editorView.setImageBitmap(photoBitmap);
  50.                     currentFilter = "nofilter";
  51.                 }
  52.             break;
  53.             case R.id.btnSepia:
  54.                 if(currentFilter != "sepia"){
  55.  
  56.                     processedBitmap = BitmapProcessing.sepia(photoBitmap);
  57.  
  58.                     editorView.setImageBitmap(processedBitmap);
  59.                     currentFilter = "sepia";
  60.                 } else{
  61.                     editorView.setImageBitmap(photoBitmap);
  62.                     currentFilter = "nofilter";
  63.                 }
  64.                 break;
  65.             case R.id.btnNegative:
  66.                 if(currentFilter != "negative") {
  67.  
  68.                     processedBitmap = BitmapProcessing.invert(photoBitmap);
  69.                     editorView.setImageBitmap(processedBitmap);
  70.                     currentFilter = "negative";
  71.                 } else {
  72.  
  73.                     editorView.setImageBitmap(photoBitmap);
  74.                     currentFilter = "nofilter";
  75.                 }
  76.             break;
  77.         }
  78.     }
  79.  
  80.     Bitmap photoBitmap;
  81.  
  82.     Bitmap processedBitmap;
  83.  
  84.     @Override
  85.     protected void onCreate(Bundle savedInstanceState) {
  86.         super.onCreate(savedInstanceState);
  87.         setContentView(R.layout.activity_editor);
  88.         //hide this ugly shit
  89.         getSupportActionBar().hide();
  90.  
  91.         editorView = (ImageView) findViewById(R.id.editorView);
  92.  
  93.         editorView.setDrawingCacheEnabled(true);
  94.  
  95.         currentFilter = "nofilter";
  96.  
  97.         findViewById(R.id.btnWithoutfilters).setOnClickListener(this);
  98.         findViewById(R.id.btnSepia).setOnClickListener(this);
  99.         findViewById(R.id.btnNegative).setOnClickListener(this);
  100.  
  101.  
  102.         Bitmap photoBM = (Bitmap) getIntent().getExtras().get("photo");
  103.  
  104.         if(photoBM != null){
  105.  
  106.             photoBitmap = photoBM.copy(photoBM.getConfig(),true);
  107.  
  108.  
  109.         } else {
  110.             Uri urlToPhoto = (Uri) getIntent().getExtras().get("photoUri");
  111.             try {
  112.                 photoBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), urlToPhoto);
  113.                 photoBitmap = photoBitmap.copy(photoBitmap.getConfig(),true);
  114.             } catch (IOException e){
  115.                 Toast.makeText(getApplicationContext(),"Не могу найти изображение",Toast.LENGTH_LONG).show();
  116.                 finish();
  117.             }
  118.         }
  119.  
  120.         if(photoBitmap != null){
  121.             editorView.setImageBitmap(photoBitmap);
  122.         } else{
  123.             Toast.makeText(getApplicationContext(),"Не могу найти изображение", Toast.LENGTH_LONG).show();
  124.             finish();
  125.         }
  126.  
  127.     }
  128.  
  129.  
  130.  
  131.  
  132. }
  133.  
  134.  
  135. /*
  136. Error message
  137.  
  138.   java.lang.IllegalStateException: Can't call getPixel() on a recycled bitmap
  139.                       at android.graphics.Bitmap.checkRecycled(Bitmap.java:347)
  140.                       at android.graphics.Bitmap.getPixel(Bitmap.java:1307)
  141.                       at by.name.photoeditor.BitmapProcessing.sepia(BitmapProcessing.java:252)
  142.                       at by.name.photoeditor.EditorActivity.onClick(EditorActivity.java:56)
  143.                       at android.view.View.performClick(View.java:4908)
  144.                       at android.view.View$PerformClick.run(View.java:20378)
  145.                       at android.os.Handler.handleCallback(Handler.java:815)
  146.                       at android.os.Handler.dispatchMessage(Handler.java:104)
  147.                       at android.os.Looper.loop(Looper.java:194)
  148.                       at android.app.ActivityThread.main(ActivityThread.java:5691)
  149.                       at java.lang.reflect.Method.invoke(Native Method)
  150.                       at java.lang.reflect.Method.invoke(Method.java:372)
  151.                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
  152.                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
  153. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement