令牌上的语法错误"(",删除此令牌

时间:2014-03-15 18:07:56

标签: java android eclipse

我正在为A-Levels的最后一年开发应用程序而且我正试图制作一个颜色平均器。我可以让应用程序拍摄照片并将其显示在imageView中,但我似乎无法从图像中获取像素。我尝试过使用“imageViewName”.getPixels或其他任何.getPixels但是getPixels之后的括号有错误“令牌上的语法错误”(“,删除此令牌”和结束括号相同。

错误发生在这里:

photoViewForCrop.getPixels(pixels [],x,y,imageWidth,imageHeight);

这是非常接近代码的结尾。

这是我拍摄照片并将其保存到SD卡的活动,请忽略它被称为“UploadPhotoActivity”,不知道我在想什么。

我的老师在弄乱它并且根本没有帮助的时候添加了代码中没有使用的导入或内容。

    package com.colours.javerager;

    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;

    import android.app.ActionBar;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Color;
    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.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class UploadPhotoActivity extends Activity {

//Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;

//directory for file names
private static final String IMAGE_DIRECTORY_NAME = "JAverager";

private Uri fileUri; //file url to store

private ImageView photoViewForCrop;
private Button takePhotoButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_photo);
    // Show the Up button in the action bar.
    //setupActionBar();
    ActionBar actionBar = getActionBar();
    // hide the action bar
    actionBar.hide();

    photoViewForCrop = (ImageView) findViewById(R.id.photoViewForCrop);
    takePhotoButton = (Button) findViewById(R.id.takePhotoButton);

    /**
     * Capture image button click event
     */
    takePhotoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // capture picture
            captureImage();
        }
    });

    if (!isDeviceSupportCamera()) {
        Toast.makeText(getApplicationContext(),
                "Sorry! Your device doesn't support camera",
                Toast.LENGTH_LONG).show();
        // will close the app if the device does't have camera
        finish();
    }
}
//checks if device has a camera
private boolean isDeviceSupportCamera() {
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save file url in bundle as it will be null on screen orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file url
    fileUri = savedInstanceState.getParcelable("file_uri");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

/**
 * Display image from a path to ImageView
 */
private void previewCapturedImage() {
    try { 
        photoViewForCrop.setVisibility(View.VISIBLE);

        // bitmap factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 2;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);

        photoViewForCrop.setImageBitmap(bitmap);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image 
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "JAverager_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;



}

int imageWidth = 640;
int imageHeight = 480;
int[] pixels = new int[307200];
int x = 1;
int y = 1;
photoViewForCrop.getPixels(pixels[], x, y, imageWidth, imageHeight);
int pixelTotal= 307200;
int valuethispixel;
int currentBlue;
int currentRed;
int currentGreen;
int totalBlue = 0;
int totalRed = 0;
int totalGreen = 0;
int currentPixel = 1;
int tempNum;
int avRed;
int avGreen;
int avBlue;

{



while(1 < pixelTotal){
    tempNum = (Integer) pixels[currentPixel];
    currentBlue = Color.blue(tempNum);
    currentRed = Color.red(tempNum);
    currentGreen = Color.green(tempNum);
    totalBlue = totalBlue + currentBlue;
    totalRed = totalRed + currentRed;
    totalGreen = totalGreen + currentGreen;
    currentPixel = currentPixel + 1;
}

totalBlue = totalBlue / pixelTotal;
totalRed = totalRed / pixelTotal;
totalGreen = totalGreen / pixelTotal;

}
    };

2 个答案:

答案 0 :(得分:0)

ImageView未实现您想要的功能。

也许你应该看看PixelGrabber(http://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html

答案 1 :(得分:0)

getOutputMediaFile方法之后的所有内容都不在方法内部,这就是为什么你的方法调用,之后while循环定义不起作用