如何捕获图像将其保存在图库中转换为阈值和灰度并将这些图像存储在本地服务器(XAMPP)中

时间:2014-05-15 09:54:37

标签: java android sql json

我需要使用相机捕捉来点击图像>将图像保存在手机图库中>将该图像转换为灰度和阈值,最后使用php,JSON和Xampp(sql)将两个转换后的图像发送到服务器上。下面是代码。我知道灰度&阈值功能正常工作。请在我出错的地方纠正我。

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.annotation.SuppressLint;
import android.app.Activity;  
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ProgressDialog;
import android.widget.TextView;
import android.widget.Toast;


public class CamActivity extends Activity 
{      
    private ImageView img;
    int boundBoxInDp;
    private static final int CAMERA_REQUEST = 1888; 
    Button bt1,bt2,btn;
    ContentValues values;
    int serverResponseCode = 0;
    ProgressDialog dialog = null;           

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cam);

        img = (ImageView)findViewById(R.id.imageView1);// Assign imageview
        BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();//get image drawable and assign to bitmap drawable
        final Bitmap bmap = drawable.getBitmap();//assign bitmap drawable to bmap which is a bitmap image variable

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();//
        bmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);// this and the line above can be ignored


        //START CAMERA ON LAUNCH OF PAGE
        Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        // Save code here
        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.png");
        camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        //Save code ends    

        startActivityForResult(camera_intent, CAMERA_REQUEST);
        //START CAMERA ON LAUNCH OF PAGE ENDS

        bt1=(Button)findViewById(R.id.button1);//button to go to next page
        bt1.setOnClickListener (new OnClickListener()//click button to travel to the next page
        {
            public void onClick(View v) 
            {
                img = (ImageView)findViewById(R.id.imageView1);
                img.setScaleType(ScaleType.MATRIX);
                BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
                drawable.setFilterBitmap(true);
                final Bitmap bmap = drawable.getBitmap();

                //convert starts WORKS 

                //convert Bitmap to grayscale 
                Bitmap imgToGreyScale;
                imgToGreyScale = toGrayscale(bmap);  

                //convert to threshold
                Bitmap imgToBlackWhite;
                imgToBlackWhite = ConvertToThreshold(imgToGreyScale); 

                //set Bitmap to imageView
                ImageView imgbit;
                imgbit = (ImageView) findViewById(R.id.imageView1);
                imgbit.getImageMatrix();
                imgbit.setImageBitmap(imgToBlackWhite);
                //              //conversion ends WORKS

                Intent intent2 = new Intent(CamActivity.this,QrCodeActivity.class);
                startActivity(intent2);


            }
        });
    }


    // FUNCTION STARTS--------------------------------------------------------------------------------------

    //Threshold Function -WORKS-
    public Bitmap ConvertToThreshold(Bitmap anythingBmap)
    {
        int width = anythingBmap.getWidth();
        int height = anythingBmap.getHeight();
        int threshold = 120;
        for(int x=0;x<width;x++){
            for(int y=0;y<height;y++){

                int pixel = anythingBmap.getPixel(x, y);
                int gray = Color.red(pixel);
                if(gray < threshold){
                    anythingBmap.setPixel(x, y, 0xFF000000);
                } else{
                    anythingBmap.setPixel(x, y, 0xFFFFFFFF);
                }
            }
        }
        return anythingBmap;
    }
    //Threshold function ends

    // Grayscale Function -WORKS-
    public Bitmap toGrayscale(Bitmap bmpOriginal){        
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();    

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }
    // Grayscale Function ends

    //FUNCTIONS ENDS----------------------------------------------------------------------------------------    

    //ONResult activity to get image
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {                   
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
        { 
            // IMAGEVIEW QUALITY WORKS and only one time temp save.
            //      

            {

                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.png")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 
                    img.setImageBitmap(bitmap);
                    String path = android.os.Environment.getExternalStorageDirectory()+ File.separator
                            + "sdcard" + File.separator + "sdcard";
                    //f.delete();
                    OutputStream outFile = null;

                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png" +"");
                    try 
                    {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);// Can Change to JPEG also
                        // the above line of code doesnt matter as it doesnt change the quality.
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        } 
    }

}

0 个答案:

没有答案