Android中的Photoshop图像混合模式

时间:2014-03-11 11:04:00

标签: android image-processing photoshop blending

经过大量搜索后,我可以在android中创建Photoshop混合模式过滤器。这是我在android中的图像混合模式的工作代码它是从android 2.1到4.4的工作,享受和任何查询的问题随时可以问:)

public class MainActivity extends Activity {
    Button btnLoadImage1, btnLoadImage2;
    TextView textSource1, textSource2;
    Button btnProcessing;
    ImageView imageResult;

    final int RQS_IMAGE1 = 1;
    final int RQS_IMAGE2 = 2;
    int blendmode=1;
    Uri source1, source2;

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

        btnLoadImage1 = (Button) findViewById(R.id.loadimage1);
        btnLoadImage2 = (Button) findViewById(R.id.loadimage2);
        textSource1 = (TextView) findViewById(R.id.sourceuri1);
        textSource2 = (TextView) findViewById(R.id.sourceuri2);
        btnProcessing = (Button) findViewById(R.id.processing);
        imageResult = (ImageView) findViewById(R.id.result);
        /*
        SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.businnes);
        Picture picture = svg.getPicture();
        Drawable drawable = svg.createPictureDrawable();
        imageResult.setImageDrawable(drawable);*/
        btnLoadImage1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RQS_IMAGE1);
            }
        });

        btnLoadImage2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RQS_IMAGE2);
            }
        });

        btnProcessing.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (source1 != null && source2 != null) {
                    Bitmap processedBitmap = ProcessingBitmap(blendmode);
                    blendmode++;
                    if(blendmode>7)
                        blendmode=1;
                    if (processedBitmap != null) {
                        imageResult.setImageBitmap(processedBitmap);
                        /*Toast.makeText(getApplicationContext(), "Done",
                                Toast.LENGTH_LONG).show();*/
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Something wrong in processing!",
                                Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Select both image!", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case RQS_IMAGE1:
                source1 = data.getData();
                textSource1.setText(source1.toString());
                break;
            case RQS_IMAGE2:
                source2 = data.getData();
                textSource2.setText(source2.toString());
                break;
            }
        }
    }

    @SuppressLint("NewApi")
    private Bitmap ProcessingBitmap(int value) {
        Bitmap bm1 = null;
        Bitmap bm2 = null;
        Bitmap newBitmap = null;

        try {
            bm1 = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(source1));
            bm2 = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(source2));

            int w;
            if (bm1.getWidth() >= bm2.getWidth()) {
                w = bm1.getWidth();
            } else {
                w = bm2.getWidth();
            }

            int h;
            if (bm1.getHeight() >= bm2.getHeight()) {
                h = bm1.getHeight();
            } else {
                h = bm2.getHeight();
            }

            Config config = bm1.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }



            newBitmap = Bitmap.createBitmap(w, h, config);
            Canvas newCanvas = new Canvas(newBitmap);

            newCanvas.drawBitmap(bm1, 0, 0, null);

            Paint paint = new Paint();
            switch (value) {
            case Key.KEYS_BLEND_DARKEN:
                Print_Toast("BLEND_DARKEN");
                paint.setXfermode(new PorterDuffXfermode(Mode.DARKEN));
                break;
            case Key.KEYS_BLEND_MULTIPLY:
                Print_Toast("BLEND_MULTIPLY");
                paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
                break;
            case Key.KEYS_BLEND_ADD:
                Print_Toast("BLEND_ADD");
                paint.setXfermode(new PorterDuffXfermode(Mode.ADD));
                break;
            case Key.KEYS_BLEND_DESOLVE:
                Print_Toast("BLEND_DESOLVE");
                paint.setXfermode(new PorterDuffXfermode(Mode.DST));
                break;

            case Key.KEYS_BLEND_DESOLVE_LIGHTEN:
                Print_Toast("BLEND_LIGHTEN");
                paint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
                break;
            case Key.KEYS_BLEND_DESOLVE_OVERLAY:

                Print_Toast("BLEND_OVERLAY");
                paint.setXfermode(new PorterDuffXfermode(Mode.OVERLAY));
                break;
            case Key.KEYS_BLEND_DESOLVE_SCREEN:
                Print_Toast("BLEND_SCREEN");
                paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
                break;


            default:
                break;
            }

            paint.setShader(new BitmapShader(bm2, TileMode.CLAMP, TileMode.CLAMP));
            /*paint.setAlpha(128);
            paint.setDither(true);
            paint.setAntiAlias(true);*/
            //paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
            newCanvas.drawRect(0, 0, bm2.getWidth(), bm2.getHeight(), paint);
            //newCanvas.drawBitmap(bm2, 0, 0, paint);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return newBitmap;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    void printlog(String tag,String value){
        Log.d(tag, value);
    }
    void Print_Toast(String value){
        Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
    }
}

这是密钥类

public class Key {
    final public static int KEYS_BLEND_DARKEN=1;
    final public static int KEYS_BLEND_MULTIPLY=2;;
    public static final int KEYS_BLEND_ADD = 3;
    public static final int KEYS_BLEND_DESOLVE = 4;
    public static final int KEYS_BLEND_DESOLVE_LIGHTEN = 5;
    public static final int KEYS_BLEND_DESOLVE_OVERLAY = 6;
    public static final int KEYS_BLEND_DESOLVE_SCREEN = 7;

}

xml文件是 activity_main

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <Button
            android:id="@+id/loadimage1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Load Image1" />

        <TextView
            android:id="@+id/sourceuri1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/loadimage1"
            android:layout_below="@+id/loadimage1"
            android:layout_marginLeft="62dp"
            android:text="TextView" />

        <Button
            android:id="@+id/loadimage2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/loadimage1"
            android:layout_below="@+id/sourceuri1"
            android:text="Load Image2" />

        <TextView
            android:id="@+id/sourceuri2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/sourceuri1"
            android:layout_below="@+id/loadimage2"
            android:text="TextView" />

        <Button
            android:id="@+id/processing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/loadimage2"
            android:layout_alignRight="@+id/loadimage2"
            android:layout_below="@+id/sourceuri2"
            android:text="Processing" />

        <ImageView
            android:id="@+id/result"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/processing"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_launcher" />

</RelativeLayout>

0 个答案:

没有答案